【问题标题】:CSS and Javascript and images in ASP.NET MVC 5 website won't display after deploymentASP.NET MVC 5 网站中的 CSS 和 Javascript 以及图像在部署后不会显示
【发布时间】:2018-07-12 12:40:47
【问题描述】:

我的问题是,在我将网站上传到在线服务器后,除了图像、CSS 和 JavaScript 之外,html 页面显示完全正常。我确切地知道为什么会发生这种情况,它与这个 stackoverflow 问题中的 Ogglas 回答有关:CSS, Images, JS not loading in IIS

基本上,我可以在 Google 调试器中看到:

<link href="/Content/css?v=oJetwWdJWV96VzkmdyDS6ZG-GSKbNSyRhMkB7__C1dQ1" rel="stylesheet">

<script src="/bundles/modernizr?v=wBEWDufH_8Md-Pbioxomt90vm6tJN2Pyy9u9zHtWsPo1"></script>

但是在/Content/bundles 之前应该有一个~,所以链接应该是

<link href="~/Content/css?v=oJetwWdJWV96VzkmdyDS6ZG-GSKbNSyRhMkB7__C1dQ1" rel="stylesheet">

<script src="~/bundles/modernizr?v=wBEWDufH_8Md-Pbioxomt90vm6tJN2Pyy9u9zHtWsPo1"></script>

~ 符号表示应用程序的根目录。

问题是我不知道如何在我的 ASP.NET MVC 5 应用程序中解决这个问题。

我的 bundleConfig.cs 文件已经如下所示:

 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 https://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/toastr.js",
                  "~/Scripts/respond.js"));

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

所以我不明白我应该在哪里进行此修改,以便在部署我的网站后,我的样式链接包含 ~ 符号。

【问题讨论】:

    标签: javascript html css asp.net asp.net-mvc-5


    【解决方案1】:

    在你的包配置中试试这个

     BundleTable.EnableOptimizations = false;
    

    【讨论】:

    • 非常感谢您,这非常有效。我做了更多研究,发现如果我将这行代码添加到我的 bundleconfig 文件中,那么我必须在每个视图上添加:@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @Styles.Render("~/Content/css")。所以现在我所有的风格都很好。但是,我的图像没有显示。路径看起来很好,所以我不知道为什么会这样?
    • 没关系,我解决了...我意识到我什至没有文件夹中的图像!大声笑
    猜你喜欢
    • 2016-04-11
    • 2013-11-28
    • 2017-01-26
    • 2018-06-17
    • 1970-01-01
    • 2018-02-01
    • 2011-07-31
    • 2020-06-16
    • 2015-10-09
    相关资源
    最近更新 更多