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

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

问题描述

我有一个ASP.Net MVC项目,我想实现捆绑,但一切我能找到在互联网上指导我打开 BundleConfig.cs App_Start - 但是这个文件不在我的项目存在。我有一个文件夹中只有三个文件:一个FilterConfig RouteConfig WebApiConfig

当我创建的解决方案(IIRC它是在开始一个空白的ASP.NET MVC项目)。

未生成

捆绑配置

看起来这应该是很容易的事,但我只是普通的不能看着办吧。

在此先感谢!

P.S。只是为了澄清那些不专心地看,这是从头开始创建一个MVC4 / .NET 4.5的应用程序。该溶液在下面标记


解决方案

BundleConfig 无非是捆绑的配置转移到单独的文件。它曾经是应用程序启动code的一部分(过滤器,捆绑,用在一个类中配置的路由)

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

 安装封装Microsoft.AspNet.Web.Optimization

然后App_Start文件夹下创建一个新的名为CS文件 BundleConfig.cs 。以下是我在我矿(ASP.NET MVC 5,但它应该与MVC 4工作):

 使用的System.Web;
使用System.Web.Optimization;命名空间codeRepository.Web
{
    公共类BundleConfig
    {
        //关于捆绑的更多信息,请访问http://go.microsoft.com/fwlink/?LinkId=301862
        公共静态无效RegisterBundles(BundleCollection包)
        {
            bundles.Add(新ScriptBundle(〜/包/ jQuery的)。包括(
                        〜/脚本/ jquery- {}版本的.js));            bundles.Add(新ScriptBundle(〜/包/ jqueryval)。包括(
                        〜/脚本/ jquery.validate *));            //使用Modernizr的开发版本,开发和借鉴。然后,当你
            //为生产做好准备,在使用的http://modernizr.com构建工具挑选您所需要的测试。
            bundles.Add(新ScriptBundle(〜/包/ Modernizr的)。包括(
                        〜/脚本/ modernizr- *));            bundles.Add(新ScriptBundle(〜/包/引导)。包括(
                      〜/脚本/ bootstrap.js
                      〜/脚本/ respond.js));            bundles.Add(新StyleBundle(〜/内容/ CSS)。包括(
                      〜/内容/ bootstrap.css
                      〜/内容/的site.css));
        }
    }
}

然后修改您的Global.asax和呼叫添加到 RegisterBundles()的Application_Start()

 使用System.Web.Optimization;保护无效的Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

一个密切相关的问题:如何引用添加到System.Web.Optimization对MVC-3转换,于─ 4应用

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.

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.

Thanks in advance!

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

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

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"));
        }
    }
}

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);
}

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

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

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