简体   繁体   中英

Bootstrap not working with ASP.net MVC Bundling

I am creating a simple login page in ASP.net MVC with bootstrap

BundleConfig.cs

public class BundleConfig
    {

        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.IgnoreList.Clear();

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

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

            bundles.Add(new StyleBundle("~/Content/bootstrapcss").Include(
                        "~/Content/bootstrap.min.css",
                        "~/Content/bootstrap.css"));
        }

_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/bootstrapcss")
</head>
<body>
    @RenderBody()
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrapjs")
</body>
</html>

View: Index.cshtml

@model ME.Models.Admin.AdminModel

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/shared/_Layout.cshtml";
}

<center>
<div class="panel panel-primary" style="display:inline-block">
    Hello Admin! Please Login
    <br />

    <table>
        <tr>
            <td>@Html.LabelFor(m=>m.username)</td>
            <td>@Html.TextBoxFor(m=>m.username, new { @class = "form-control" })<br /></td>
        </tr>
        <tr>
            <td>@Html.LabelFor(m=>m.password)</td>
            <td>@Html.TextBoxFor(m=>m.password, new { @class = "form-control" })<br /></td>

        </tr>
        <tr>
            <td></td>
            <td><input type="submit" class="btn btn-primary" value="Log In" /></td>
        </tr>
    </table>

</div>
    </center>

On running the project, Controls are displayed but bootstrap classes are not applied to controls.

Under browser console I get 404 error for bundles:

/bundles/jquery
/bundles/bootstrapjs
/Content/bootstrapcss

I have used bundling and _Layout.cshtml in Shared directory under view folder.

Can someone help with this issue?

Note : I haven't pasted code of Model and controller since I believe it's not needed here.

Thanks.

protected virtual void Application_Start()
    {
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

This was missing. Got it worked.

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