简体   繁体   English

MVC 4中的CSS捆绑

[英]CSS bundling in MVC 4

I'm building a web application in MVC 4. I'm using bootstrap to design my UI. 我正在MVC 4中构建Web应用程序。我正在使用引导程序来设计UI。 I have included my bootstrap files to the BundleConfig.cs file on my solution to reduce file size and optimize my site responsiveness and load speed. 我已将引导程序文件包含在解决方案中的BundleConfig.cs文件中,以减小文件大小并优化站点的响应速度和加载速度。 Rendering a view page and referencing the bundle name doesn't seem to output the required result for me. 渲染视图页面并引用包名称似乎无法为我输出所需的结果。

I tried referencing bootstrap through CDN and it works so I think the fault is in the bundling. 我尝试通过CDN引用引导程序,它可以正常工作,所以我认为错误在于捆绑。 However I cannot figure out the faults. 但是,我无法找出问题所在。 Any help is appreciated. 任何帮助表示赞赏。 Thank You. 谢谢。

BundleConfig.cs BundleConfig.cs

using System.Web;
using System.Web.Optimization;

namespace TMS
{
    public class BundleConfig
    {
        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.IgnoreList.Clear();

        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
            "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrapjs").Include(
            "~/Scripts/bootstrap.min.js"));

        bundles.Add(new StyleBundle("~/Content/BootstrapFiles").Include(
            "~/Content/bootstrap.min.css",
            "~/Content/bootstrap-responsive.min.css",
            "~/Content/bootstrap.css",
            "~/Content/bootstrap-responsive.css"));

        //BundleTable.EnableOptimizations = true;
    }
}
}

_Layout.cshtml _Layout.cshtml

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
<!--    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> -->
    @Styles.Render("~/Content/BootstrapFiles")
</head>

and in the body of the page i'm trying to have navbar that is black and aligned to the right 在页面的主体中,我试图将导航栏设置为黑色并向右对齐

<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" href="#">Welcome to Sri Lanka!</a>
        </div>

        <ul id="menu" class="nav navbar-nav navbar-right">
            <li>@Html.ActionLink("Home", "Index", "Home")</li>
            <li>@Html.ActionLink("About", "About", "Home")</li>
            <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
        </ul>
    </div>
</nav>

PS I've also set the debug mode false in my main web.config file PS我还在主web.config文件中将调试模式设置为false

you need to call RegisterBundles when your Application start from global ajax 当您的应用程序从全局ajax启动时,您需要调用RegisterBundles

like. 喜欢。

in Global.ajax.cs file 在Global.ajax.cs文件中

 protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);//for register route
        BundleConfig.RegisterBundles(BundleTable.Bundles);//for register bundle
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM