Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CSS束中的相对路径转换_C#_Css_Asp.net Mvc_Asp.net Mvc 5 - Fatal编程技术网

C# CSS束中的相对路径转换

C# CSS束中的相对路径转换,c#,css,asp.net-mvc,asp.net-mvc-5,C#,Css,Asp.net Mvc,Asp.net Mvc 5,我正试图为我的MVC5项目捆绑CSS文件,但我遇到了相对URL的问题。在你捣碎重复问题按钮之前,我已经阅读了其他类似的问题,而答案并没有涉及我需要处理的特定情况 我正在处理的项目将在常规IIS站点下作为应用程序运行,这意味着它不会位于域的根目录下。当使用现成的URL转换CSSRWriteUrlTransform时,这是一个问题,因为在我看来,它愚蠢地没有考虑URL中的应用程序名称,而是将域根作为起始级别来生成绝对路径。假设我有以下文件夹结构: site '--- content |--

我正试图为我的MVC5项目捆绑CSS文件,但我遇到了相对URL的问题。在你捣碎重复问题按钮之前,我已经阅读了其他类似的问题,而答案并没有涉及我需要处理的特定情况

我正在处理的项目将在常规IIS站点下作为应用程序运行,这意味着它不会位于域的根目录下。当使用现成的URL转换CSSRWriteUrlTransform时,这是一个问题,因为在我看来,它愚蠢地没有考虑URL中的应用程序名称,而是将域根作为起始级别来生成绝对路径。假设我有以下文件夹结构:

site
'--- content
     |--- css
     |    '--- style.css
     '--- images
          '--- logo.png
如果我使用这样的相对URL:

background-image: url('../images/logo.png');
它被转换为:

background-image: url('http://domainname.com/content/images/logo.png');
问题是,我需要它在进行转换时尊重应用程序/子目录名称。解决方案还应该保留查询字符串

background-image: url('http://domainname.com/applicationname/content/images/logo.png[?querystring]
我找到的最接近的是AcidPAT在这个问题上提供的解决方案:

但这个解决方案并没有为我编译,因为它似乎将response.Files视为IEnumerable,而实际上它是IEnumerable—可能这从MVC4更改为MVC5

总结

我需要一种在绑定CSS时自动将CSS中的相对路径转换为绝对路径的方法。解决方案需要了解应用程序的实际位置,因此,如果应用程序安装在域的子目录下,那么它应该可以工作。解决方案还需要保留相对URL中的任何查询字符串

有人能提供一些指导吗

最接近的解决方案是这个要点:唯一的问题是它似乎与MVC5不兼容。特别是从第12行开始的块,它开始遍历BundleFile对象列表,似乎将它们视为FileInfo对象,因此正在调用不存在的属性。

。它将解析主机,但不会解析虚拟目录。这是我用的。它只是一个允许捆绑过程正确解析的包装器类

public class CssRewriteUrlTransformWrapper : IItemTransform
{
    public string Process(string includedVirtualPath, string input)
    {
        return new CssRewriteUrlTransform().Process("~" + VirtualPathUtility.ToAbsolute(includedVirtualPath), input);
    }
}
用法

bundles.Add(new StyleBundle("~/Content/css")
       .Include("~/Content/Site.css", new CssRewriteUrlTransformWrapper()));
。它将解析主机,但不会解析虚拟目录。这是我用的。它只是一个允许捆绑过程正确解析的包装器类

public class CssRewriteUrlTransformWrapper : IItemTransform
{
    public string Process(string includedVirtualPath, string input)
    {
        return new CssRewriteUrlTransform().Process("~" + VirtualPathUtility.ToAbsolute(includedVirtualPath), input);
    }
}
用法

bundles.Add(new StyleBundle("~/Content/css")
       .Include("~/Content/Site.css", new CssRewriteUrlTransformWrapper()));

请记住,css中的url必须用引号括起来请记住,css中的url必须用引号括起来