加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

手动升级后,将新的ASP.NET Web Optimization框架添加到MVC4项目

发布时间:2020-12-15 23:13:25 所属栏目:asp.Net 来源:网络整理
导读:在将ASP.NET MVC项目手动升级到MVC4 using these instructions后,如何在MVC4中设置ASP.NET Web Optimization Framework的新CSS和JavaScript资产捆绑和最小化功能?默认模板全部设置完成,但是如何手动执行? 解决方法 右键单击“引用”,然后管理NuGet软件包,
在将ASP.NET MVC项目手动升级到MVC4 using these instructions后,如何在MVC4中设置ASP.NET Web Optimization Framework的新CSS和JavaScript资产捆绑和最小化功能?默认模板全部设置完成,但是如何手动执行?

解决方法

>右键单击“引用”,然后管理NuGet软件包,并添加“Microsoft.AspNet.Web.Optimization”(或在NuGet控制台中键入Install-Package Microsoft.AspNet.Web.Optimization).
>在您的Web.config文件中,将以下内容添加到< system.webServer>中,允许使用扩展名URL提供最小化的捆绑包.
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFrameworkv4.0.30319aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFramework64v4.0.30319aspnet_isapi.dll" preCondition="classicMode,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

>在您的App_Start文件夹中,添加一个名为BundleConfig.cs的新类.它应该看起来像这样:

using System.Web;
using System.Web.Optimization;

namespace MvcApplication1
{
    public class BundleConfig
    {
        // For more information on Bundling,visit http://go.microsoft.com/fwlink/?LinkId=254725
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                "~/Scripts/jquery-ui-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                "~/Scripts/jquery.unobtrusive*","~/Scripts/jquery.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then,when you're
            // ready for production,use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                        "~/Content/themes/base/jquery.ui.core.css"));
        }
    }
}

>编辑上面的内容以添加所需的脚本和样式表包,然后在Global.asax.cs中的using部分和Application_Start中添加以下行:

//using section
using System.Web.Optimization;

//Application_Start
BundleConfig.RegisterBundles(BundleTable.Bundles);

>将_Layout.cshtml中的CSS和JavaScript和标签替换为@ Styles.Render(“?/ Content / css”)和@ Scripts.Render(“?/ bundles / jquery”),将参数替换为您添加到BundleConfig.cs的捆绑包.确保不要将与项目中的文件夹相同的任何包命名.

您现在应该全部设置 – 请阅读如何使用完整的功能集:http://www.asp.net/mvc/overview/performance/bundling-and-minification

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读