Asp.net MVC5如何添加BundleConfig.cs到我的项目

在新建一个空MVC项目时是没有BundleConfig的,但是又想用怎么办呢?

要添加此文件,首先需要将Microsoft.AspNet.Web.Optimization nuget包添加到您的web项目:

Install-Package Microsoft.AspNet.Web.Optimization

然后在App_Start文件夹下创建一个名为BundleConfig.cs的新cs文件。

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

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

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/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 ScriptBundle("~/bundles/bootstrap").Include(
                      "~/Scripts/bootstrap.js",
                      "~/Scripts/respond.js"));

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

然后修改您的Global.asax并在Application_Start()中添加一个对RegisterBundles()的调用:

using System.Web.Optimization;

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

在页面中调用,如果提示找不到Styles

 

就到web.config中添加一行
<add namespace="System.Web.Optimization" />
 
 
原文地址:https://www.cnblogs.com/ewyb/p/12186322.html