BundleConfig
无非就是将捆绑软件配置移到了单独的文件中。它曾经是应用程序启动代码的一部分(用于在一类中配置的过滤器,包,路由)
要添加此文件,首先需要将Microsoft.AspNet.Web.Optimization
nuget包添加到您的Web项目中:
Install-Package Microsoft.AspNet.Web.Optimization
然后在App_Start文件夹下创建一个名为的新cs文件BundleConfig.cs
。这是我所拥有的(ASP.NET MVC 5,但它应可与MVC 4一起使用):
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和呼叫添加RegisterBundles()
在Application_Start()
:
using System.Web.Optimization;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
一个密切相关的问题:如何为MVC-3-converted-to-4应用程序添加对System.Web.Optimization的引用