MVC的捆绑在使用CssRewriteUrlTransform时返回CSS图像中的错误URL:
我有一个内部网应用程序的URL是,例如:http:// usid01-srv002 / MyApplication。它在IIS的“默认网站”。
其中在BundleConfig.cs中有以下:
bundles.Add(new StyleBundle("~/bundles/jcss") .Include("~/Scripts/JQueryUI/css/*.css",new CssRewriteUrlTransform()) );
绑定系统正在为这些CSS文件中引用的任何图像生成错误的URL,生成404的甚至JQueryUI的经过良好测试的CSS文件(来自FireBug):
例如它产生
http://usid01/path/foo.png
当它应该产生:
http://usid01/MyApplication/path/foo.png
如何使捆绑系统生成指向正确位置的URL?
解决方法
CssRewriteUrlTransform用绝对路径更新CSS网址,如果我们使用 –
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
绑定将生成以下CSS Url路径“background-image” –
background-image: url("/Content/Images/Sandy.jpg");
现在如果你托管的网站/ Web应用程序作为一个网站上面的Web服务器上面的路径将工作,
因为浏览器将使用以下网址发送此资源的请求,因为前导’/’
http://<server>/content/images/sandy.jpg
但如果你托管网站作为Web应用程序这将创建问题。因为浏览器仍然会将此解释为绝对Url而不是相对的,并仍然发送以下请求来获取此资源 –
http://<server>/content/images/sandy.jpg
所以,这个问题的解决方案是使用相对Url甚至在CSS文件,然后从Bundle配置中删除CssRewriteUrlTransform如下 –
background-image: url("Images/Sandy.jpg"); bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。