asp.net-mvc 如何将 BundleConfig.cs 添加到我的项目中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21668280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add BundleConfig.cs to my project?
提问by Maverick
I have an ASP.Net MVC project and I want to implement bundling, but everything I can find on the internet directs me to open BundleConfig.csin App_Start- however this file does not exist in my project. I have only three files in that folder: FilterConfig, RouteConfigand WebApiConfig.
我有一个ASP.Net MVC项目,我想实现捆绑,但一切我可以在互联网上找到指引我开BundleConfig.cs的App_Start-但是这个文件不在我的项目存在。我在该文件夹中只有三个文件:FilterConfig,RouteConfig和WebApiConfig.
Bundle config wasn't generated when I created the solution (IIRC it was a blank ASP.NET MVC project at the beginning).
我创建解决方案时没有生成捆绑配置(IIRC 一开始是一个空白的 ASP.NET MVC 项目)。
It seems like this should be really easy to do, but I just plain cannot figure it out.
看起来这应该很容易做到,但我只是无法弄清楚。
P.S. Just to clarify to those not reading closely, this is for a MVC4/.Net 4.5 app created from scratch. The solution is marked below.
PS 只是为了向那些没有仔细阅读的人澄清,这是针对从头开始创建的 MVC4/.Net 4.5 应用程序。解决方案标记如下。
回答by vmg
BundleConfigis nothing more than bundle configuration moved to separate file. It used to be part of app startup code (filters, bundles, routes used to be configured in one class)
BundleConfig只不过是捆绑配置移动到单独的文件。它曾经是应用程序启动代码的一部分(过去在一个类中配置过滤器、捆绑包、路由)
To add this file, first you need to add the Microsoft.AspNet.Web.Optimizationnuget package to your web project:
要添加此文件,首先需要将Microsoft.AspNet.Web.Optimizationnuget 包添加到您的 Web 项目中:
Install-Package Microsoft.AspNet.Web.Optimization
Then under the App_Start folder create a new cs file called BundleConfig.cs. Here is what I have in my mine (ASP.NET MVC 5, but it should work with MVC 4):
然后在 App_Start 文件夹下创建一个名为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"));
}
}
}
Then modify your Global.asax and add a call to RegisterBundles()in Application_Start():
然后修改您的 Global.asax 并添加对RegisterBundles()in的调用Application_Start():
using System.Web.Optimization;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
A closely related question: How to add reference to System.Web.Optimization for MVC-3-converted-to-4 app
一个密切相关的问题:How to add reference to System.Web.Optimization for MVC-3-converted-to-4 app
回答by Cesar Alvarado Diaz
If you are using "MVC 5" you may not see the file, and you should follow these steps: http://www.techjunkieblog.com/2015/05/aspnet-mvc-empty-project-adding.html
如果您使用的是“MVC 5”,您可能看不到该文件,您应该按照以下步骤操作:http: //www.techjunkieblog.com/2015/05/aspnet-mvc-empty-project-adding.html
If you are using "ASP.NET 5" it has stopped using "bundling and minification" instead was replaced by gulp, bower, and npm. More information see https://jeffreyfritz.com/2015/05/where-did-my-asp-net-bundles-go-in-asp-net-5/
如果您使用的是“ASP.NET 5”,它已停止使用“捆绑和缩小”,取而代之的是 gulp、bower 和 npm。更多信息请参见https://jeffreyfritz.com/2015/05/where-did-my-asp-net-bundles-go-in-asp-net-5/

