CssRewriteUrlTransform可以更新CSS URL为绝对路径,意味着如果我们使用-
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css",new CssRewriteUrlTransform()));
而我们在 "site.css" 文件中有以下的 CSS 类:
.Sandy
{
background-image: url("Images/Sandy.jpg");
border: 1px solid #c8c8c8;
border-radius:4px 4px 4px 4px;
box-shadow: 1px 1px 8px gray;
background-position:left;
background-size:contain;
-moz-background-size:contain;
-webkit-background-size:contain;
-o-background-size:contain;
background-repeat:no-repeat;
min-height:100px;
min-width:100px;
display:block;
}
并且以下是文件夹结构 -
-Web site Root
-Content
--site.css
--Images
---Sandy.jpg
打包将为“background-image”生成以下CSS URL路径 -
background-image: url("/Content/Images/Sandy.jpg");
如果您将网站/ Web应用程序托管为 Web 服务器上的网站,则以上路径将起作用,
因为浏览器将使用以下 URL 发送对此资源的请求,因为它以斜杠开头
http://<server>/content/images/sandy.jpg
但是,如果您将网站托管为Web应用程序,则会创建问题。因为浏览器仍将其解释为绝对URL而不是相对URL,并且仍会发送以下请求以获取此资源 -
http://<server>/content/images/sandy.jpg
所以,解决这个问题的方法是即使在CSS文件中也使用相对URL,然后从Bundle配置中删除CssRewriteUrlTransform,如下所示 -
background-image: url("Images/Sandy.jpg");
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
Muhammad Waqas Iqbal
回答的答案... 参见:https://dev59.com/VGgu5IYBdhLWcg3wZmMm - Jonathan