MVC 4 中 Styles.Render 和 Scripts.Render 的澄清说明

3
我正在使用ASP.NET MVC 4和Bootstrap,我发现我的项目与许多“使用Bootstrap构建站点”的文章和教程略有不同,它们一直提到使用压缩的Javascript和CSS文件(bootstrap.min.css,bootstrap.min.js),而我只有没有min的文件,尽管这些文件在目录中。
我了解过这些文件,但我不知道它们的用途,也许有人可以澄清一下它们的用途,以及是否需要在我的bundle配置中添加引用?
我的代码看起来像这样:
_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("~/Content/css").Include()


#1:在部署产品时,使用min.js并不会有任何影响。 #2:好的,打包文件!是的,我个人更喜欢这样做,因为我们不知道在项目中引用了多少JS文件。但是你可以将它们添加到捆绑包中并使用它们。干杯! - super cool
谢谢,这是一个很好的问题,帮了我很多。 - Lior Erez
1个回答

1

min文件会去掉所有不必要的字符,使得文件更小,这样可以减少加载时间。源代码

如果你正在构建一个小型个人项目,性能可能不是首要考虑因素。如果你是为别人或组织构建网站,我建议使用min版本。


你是指减少加载时间吗? 那么,如果我想使用较小的文件(.min.),我将把bootstrap.js的“include”替换为bootstrap.min.js?就这么简单? 我不需要保留'bootstrap.js'吗?只需替换它即可? - Ron
1
bootstrap.js和bootstrap.min.js包含完全相同的信息。不同之处在于bootstrap.js对于我们人类来说更易读,而bootstrap.min.js对于计算机来说更易读。用bootstrap.min.js替换bootstrap.js是安全的。 - Simon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接