如何将 BundleConfig.cs 添加到我的项目中? [英] How do I add BundleConfig.cs to my project?

查看:31
本文介绍了如何将 BundleConfig.cs 添加到我的项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ASP.Net MVC 项目,我想实现捆绑,但是我在互联网上可以找到的所有内容都指导我在 App_Start 中打开 BundleConfig.cs -但是这个文件在我的项目中不存在.我在那个文件夹中只有三个文件:FilterConfigRouteConfigWebApiConfig.

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.

我创建解决方案时未生成捆绑配置(IIRC 一开始是一个空白的 ASP.NET MVC 项目).

Bundle config wasn't generated when I created the solution (IIRC it was a blank ASP.NET MVC project at the beginning).

这看起来应该很容易做到,但我就是想不通.

It seems like this should be really easy to do, but I just plain cannot figure it out.

附言只是为了向那些没有仔细阅读的人澄清,这是针对从头开始创建的 MVC4/.Net 4.5 应用程序.解决方案标记如下.

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.

推荐答案

BundleConfig 只不过是将捆绑配置移至单独的文件.它曾经是应用启动代码的一部分(过去在一个类中配置过滤器、捆绑包、路由)

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)

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

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

Install-Package Microsoft.AspNet.Web.Optimization

然后在 App_Start 文件夹下创建一个名为 BundleConfig.cs 的新 cs 文件.这是我的(ASP.NET MVC 5,但它应该适用于 MVC 4):

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):

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() 的调用:

Then modify your Global.asax and add a call to RegisterBundles() in Application_Start():

using System.Web.Optimization;

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

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

A closely related question: How to add reference to System.Web.Optimization for MVC-3-converted-to-4 app

这篇关于如何将 BundleConfig.cs 添加到我的项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆