Skip to main content
 首页 » 编程设计

c#之从 Visual Studio 2015 在 Azure 中发布应用程序不会加载 .CSS

2024年02月05日11myhome

我在 Azure 中从 Visual Studio 2015 发布我的应用程序时遇到了这个问题;它看起来像这样:

显然它没有正确加载 .css 文件(谁知道还有什么)。

我做了很多调查,但与我的预期相反,很难找到问题的根源,但是在一些丢失的论坛中我发现 web.config 在发布时更改了它的属性,我的本地web.config 看起来像这样:

<system.web> 
<authentication mode="None" /> 
<compilation debug="true" targetFramework="4.5.2" /> 
<httpRuntime targetFramework="4.5.2" /> 
<httpModules> 
  <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> 
</httpModules> 

我的远程 web.config 是这样的:

<system.web> 
<authentication mode="None" /> 
<compilation targetFramework="4.5.2" /> 
<httpRuntime targetFramework="4.5.2" /> 
<httpModules> 
  <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> 
</httpModules> 

当我在编译中添加 debug="true"时,它工作正常:

我想把这个留在这里是为了帮助其他遇到同样问题的人,顺便说一下我的问题:

1- 为什么禁用调试会影响 .css 文件(可能还有其他文件)的加载方式?

2- 是否有另一种方法可以在不允许在我的远程服务器上进行调试的情况下更正此问题?

3- 也许还有另一种解决方法,但我在寻求帮助时错过了,如果有人以前遇到过这种情况,请分享您的知识。

可以预料这会很常见,毕竟我只是在 azure xD 中发布一个应用程序
值得一提的是,我的应用程序还可以与数据库一起使用,我现在将尝试学习如何在 azure xD 上实现它

谢谢!

编辑: BundleConfig.cs

using System.Web; 
using System.Web.Optimization; 
 
namespace Distribuidora_Saturno 
{ 
    public class BundleConfig 
    { 
        // Para obtener más información sobre Bundles, visite 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*")); 
 
            // Utilice la versión de desarrollo de Modernizr para desarrollar y obtener información. De este modo, estará 
            // preparado para la producción y podrá utilizar la herramienta de compilación disponible en http://modernizr.com para seleccionar solo las pruebas que necesite. 
            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/CSS/bootstrap.css", 
                      "~/Content/CSS/site.css")); 
        } 
    } 
} 

请您参考如下方法:

我想我知道发生了什么。因此,我访问了您的网站并在 Chrome 开发人员工具中监控请求/响应,对于您的 CSS 包,我注意到 IIS 正在响应 403 错误。

出于好奇,我访问了捆绑链接,这就是我得到的:

所以我在捆绑文件上搜索了 403 错误并遇到了这个线程:ASP.NET MVC framework 4.5 CSS bundles does not work on the hosting它的建议是更改您的包的名称,因为它与您服务器上的物理路径冲突。

您可以尝试更改包名称吗?尝试类似的东西:

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

然后将 View 页面中的路径从 /Content/CSS 更改为 /CSSBundle。

这应该可以解决问题。