MVC 4 Styles.Render& Scripts.Render澄清

时间:2014-06-08 11:46:38

标签: twitter-bootstrap asp.net-mvc-4 scriptbundle stylebundle

我正在使用带有Bootstrap的ASP.NET MVC 4,我注意到我的项目与很多关于使用Bootstrap构建您的网站的文章和教程略有不同&#34 ;,他们继续提到使用Javascript和CSS的缩小文件(bootstrap.min.css,bootstrap.min.js),虽然文件在目录中,但我只有没有min的文件。

我读到了这些文件,但我不明白这一点,也许有人可以澄清它们的使用,如果我需要在我的捆绑配置中添加对它们的引用?

我的代码如下所示:

_Layout.cshtml:

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>

                <a href="@Url.Action("Index", "Home")" class="navbar-brand">
                    <span class="glyphicon glyphicon-filter"></span> SelectorIT - OnLine
                </a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                </ul>
                @Html.Partial("_LoginPartial")
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>


我的 BundleConfig.cs:

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

        // Set EnableOptimizations to false for debugging. For more information,
        // visit http://go.microsoft.com/fwlink/?LinkId=301862
        BundleTable.EnableOptimizations = true;
    }
}

我有两个问题:

  1. 当我没有引用bootstrap.min.css,bootstrap.min.js时,我错过了什么?
  2. 我应该在:

    中添加对这些文件的引用

    bundles.Add(new StyleBundle(&#34;〜/ Content / css&#34;)。Include()

1 个答案:

答案 0 :(得分:1)

min文件删除所有不必要的字符以使文件变小。这减少了加载时间。 Source

如果您正在构建一个小型个人项目,性能可能没有高优先级。如果您正在为其他人或组织构建网站,我建议您使用最低版本。