简体   繁体   中英

CSS bundling in MVC 4

I'm building a web application in MVC 4. I'm using bootstrap to design my 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. 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. However I cannot figure out the faults. Any help is appreciated. Thank You.

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

<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

you need to call RegisterBundles when your Application start from global ajax

like.

in Global.ajax.cs file

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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