如何添加BundleConfig。cs对我的项目吗?

时间:2023-01-13 12:02:53

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.cs in App_Start - however this file does not exist in my project. I have only three files in that folder: FilterConfig, RouteConfig and WebApiConfig.

我有一个ASP。Net MVC项目和我想实现绑定,但是我在互联网上找到的所有东西都指引我打开BundleConfig。但是这个文件在我的项目中不存在。我在那个文件夹中只有三个文件: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).

创建解决方案时没有生成Bundle config (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.

注:为了澄清那些没有仔细阅读的人,这是一个MVC4/。Net 4.5应用程序从零开始创建。解决方案如下所示。

2 个解决方案

#1


142  

BundleConfig is 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不过是移动到独立文件的bundle配置。它曾经是应用程序启动代码的一部分(过滤器、捆绑包、用于在一个类中配置的路由)

To add this file, first you need to add the Microsoft.AspNet.Web.Optimization nuget package to your web project:

要添加这个文件,首先需要添加Microsoft.AspNet.Web。优化nuget包到您的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的新的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():

然后修改你的全球。asax和在Application_Start()中向registerbundle()添加一个调用:

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

一个密切相关的问题:如何添加对System.Web的引用。优化MVC-3-converted-to-4应用

#2


1  

If using "MVC 5" not see the file, you should follow these steps link: http://www.techjunkieblog.com/2015/05/aspnet-mvc-empty-project-adding.html

如果使用“MVC 5”不查看文件,您应该遵循以下步骤链接:http://www.techjunkieblog.com/2015/05/aspnet- empty-project- add.html

If you are using "ASP.NET 5" he was stopped using "bundling and minification" instead was replaced by gulp, bower, and npm. More information see http://www.jeffreyfritz.com/2015/05/where-did-my-asp-net-bundles-go-in-asp-net-5/

如果您正在使用“ASP”。NET 5“他不再使用“捆绑和缩小”,取而代之的是“大口喝”、“低头喝”和“npm”。更多信息参见http://www.jeffreyfritz.com/2015/05/where-did-my-asp-net-bundles-go-in-asp-net-5/

#1


142  

BundleConfig is 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不过是移动到独立文件的bundle配置。它曾经是应用程序启动代码的一部分(过滤器、捆绑包、用于在一个类中配置的路由)

To add this file, first you need to add the Microsoft.AspNet.Web.Optimization nuget package to your web project:

要添加这个文件,首先需要添加Microsoft.AspNet.Web。优化nuget包到您的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的新的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():

然后修改你的全球。asax和在Application_Start()中向registerbundle()添加一个调用:

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

一个密切相关的问题:如何添加对System.Web的引用。优化MVC-3-converted-to-4应用

#2


1  

If using "MVC 5" not see the file, you should follow these steps link: http://www.techjunkieblog.com/2015/05/aspnet-mvc-empty-project-adding.html

如果使用“MVC 5”不查看文件,您应该遵循以下步骤链接:http://www.techjunkieblog.com/2015/05/aspnet- empty-project- add.html

If you are using "ASP.NET 5" he was stopped using "bundling and minification" instead was replaced by gulp, bower, and npm. More information see http://www.jeffreyfritz.com/2015/05/where-did-my-asp-net-bundles-go-in-asp-net-5/

如果您正在使用“ASP”。NET 5“他不再使用“捆绑和缩小”,取而代之的是“大口喝”、“低头喝”和“npm”。更多信息参见http://www.jeffreyfritz.com/2015/05/where-did-my-asp-net-bundles-go-in-asp-net-5/