zoukankan      html  css  js  c++  java
  • Asp.net MVC5如何添加BundleConfig.cs到我的项目

    在新建一个空MVC项目时是没有BundleConfig的,但是又想用怎么办呢?

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

    Install-Package Microsoft.AspNet.Web.Optimization

    然后在App_Start文件夹下创建一个名为BundleConfig.cs的新cs文件。

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

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

    在页面中调用,如果提示找不到Styles

     

    就到web.config中添加一行
    <add namespace="System.Web.Optimization" />
     
     
  • 相关阅读:
    Bootstrap UI层收藏介绍
    你为什么离开上家公司?三大经典面试问题剖析
    浅谈常用的Web安全技术手段
    C#中yield关键字理解
    中小型研发团队架构实践三要点(转自原携程架构师张辉清)
    你确实应该学习并使用的 10 个 C# 特性
    ASP.NET MVC 异步Excel数据选择导出
    表格中控制tr的display:block在火狐中显示错乱的解决方法
    切图笔记
    表单验证jquery.validate
  • 原文地址:https://www.cnblogs.com/ewyb/p/12186322.html
Copyright © 2011-2022 走看看