MVC4 StyleBundle无法解析图像

时间:2012-07-06 04:39:43

标签: css jquery-ui asp.net-mvc-4 bundle asp.net-optimization

我的问题与此类似:

  

ASP.NET MVC 4 Minification & Background Images

除非我想坚持使用MVC自己的捆绑,如果可以的话。我正在脑力崩溃试图弄清楚用于指定样式包的正确模式是什么,以便独立的css和图像集(如jQuery UI)工作。

我有一个典型的MVC网站结构/Content/css/,其中包含我的基本CSS,例如styles.css。在该css文件夹中,我还有/jquery-ui等子文件夹,其中包含CSS文件和/images文件夹。 jQuery UI CSS中的图像路径与该文件夹相关,我不想搞砸它们。

据我了解,当我指定StyleBundle时,我需要指定一个虚拟路径,该路径也不匹配真实内容路径,因为(假设我忽略了到内容的路由),IIS会尝试将该路径解析为物理文件。所以我指的是:

bundles.Add(new StyleBundle("~/Content/styles/jquery-ui")
       .Include("~/Content/css/jquery-ui/*.css"));

使用以下方式呈现:

@Styles.Render("~/Content/styles/jquery-ui")

我可以看到请求:

http://localhost/MySite/Content/styles/jquery-ui?v=nL_6HPFtzoqrts9nwrtjq0VQFYnhMjY5EopXsK8cxmg1

这将返回正确的缩小的CSS响应。 但随后浏览器发送相对链接图像的请求:

http://localhost/MySite/Content/styles/images/ui-bg_highlight-soft_100_eeeeee_1x100.png

这是404

我知道我的网址jquery-ui的最后一部分是一个无扩展名的网址,我的包的处理程序,所以我可以看到为什么图像的相对请求只是/styles/images/

所以我的问题是处理这种情况的正确方法是什么?

15 个答案:

答案 0 :(得分:356)

根据MVC4 css bundling and image references上的此帖子,如果您将捆绑包定义为:

bundles.Add(new StyleBundle("~/Content/css/jquery-ui/bundle")
                   .Include("~/Content/css/jquery-ui/*.css"));

如果您在与构成捆绑包的源文件相同的路径上定义捆绑包,则相对图像路径仍然有效。捆绑路径的最后一部分实际上是该特定捆绑的file name(即/bundle可以是您喜欢的任何名称。)

这只有在你将同一个文件夹中的CSS捆绑在一起时才会起作用(我认为从捆绑的角度看是有意义的。)

<强>更新

根据@Hao Kung的评论,现在可以通过应用CssRewriteUrlTransformationChange relative URL references to CSS files when bundled)来实现。

注意:我还没有确认有关重写虚拟目录中绝对路径的问题的评论,因此这可能不适合所有人(?)。

bundles.Add(new StyleBundle("~/Content/css/jquery-ui/bundle")
                   .Include("~/Content/css/jquery-ui/*.css",
                    new CssRewriteUrlTransform()));

答案 1 :(得分:33)

Grinn / ThePirat解决方案效果很好。

我不喜欢它在bundle上新的Include方法,并且它在内容目录中创建了临时文件。 (他们最终被检入,部署,然后服务无法启动!)

所以为了遵循Bundling的设计,我选择执行基本相同的代码,但是在IBundleTransform实现中::

class StyleRelativePathTransform
    : IBundleTransform
{
    public StyleRelativePathTransform()
    {
    }

    public void Process(BundleContext context, BundleResponse response)
    {
        response.Content = String.Empty;

        Regex pattern = new Regex(@"url\s*\(\s*([""']?)([^:)]+)\1\s*\)", RegexOptions.IgnoreCase);
        // open each of the files
        foreach (FileInfo cssFileInfo in response.Files)
        {
            if (cssFileInfo.Exists)
            {
                // apply the RegEx to the file (to change relative paths)
                string contents = File.ReadAllText(cssFileInfo.FullName);
                MatchCollection matches = pattern.Matches(contents);
                // Ignore the file if no match 
                if (matches.Count > 0)
                {
                    string cssFilePath = cssFileInfo.DirectoryName;
                    string cssVirtualPath = context.HttpContext.RelativeFromAbsolutePath(cssFilePath);
                    foreach (Match match in matches)
                    {
                        // this is a path that is relative to the CSS file
                        string relativeToCSS = match.Groups[2].Value;
                        // combine the relative path to the cssAbsolute
                        string absoluteToUrl = Path.GetFullPath(Path.Combine(cssFilePath, relativeToCSS));

                        // make this server relative
                        string serverRelativeUrl = context.HttpContext.RelativeFromAbsolutePath(absoluteToUrl);

                        string quote = match.Groups[1].Value;
                        string replace = String.Format("url({0}{1}{0})", quote, serverRelativeUrl);
                        contents = contents.Replace(match.Groups[0].Value, replace);
                    }
                }
                // copy the result into the response.
                response.Content = String.Format("{0}\r\n{1}", response.Content, contents);
            }
        }
    }
}

然后将其包装在Bundle Implemetation中:

public class StyleImagePathBundle 
    : Bundle
{
    public StyleImagePathBundle(string virtualPath)
        : base(virtualPath)
    {
        base.Transforms.Add(new StyleRelativePathTransform());
        base.Transforms.Add(new CssMinify());
    }

    public StyleImagePathBundle(string virtualPath, string cdnPath)
        : base(virtualPath, cdnPath)
    {
        base.Transforms.Add(new StyleRelativePathTransform());
        base.Transforms.Add(new CssMinify());
    }
}

样本用法:

static void RegisterBundles(BundleCollection bundles)
{
...
    bundles.Add(new StyleImagePathBundle("~/bundles/Bootstrap")
            .Include(
                "~/Content/css/bootstrap.css",
                "~/Content/css/bootstrap-responsive.css",
                "~/Content/css/jquery.fancybox.css",
                "~/Content/css/style.css",
                "~/Content/css/error.css",
                "~/Content/validation.css"
            ));

这是RelativeFromAbsolutePath的扩展方法:

   public static string RelativeFromAbsolutePath(this HttpContextBase context, string path)
    {
        var request = context.Request;
        var applicationPath = request.PhysicalApplicationPath;
        var virtualDir = request.ApplicationPath;
        virtualDir = virtualDir == "/" ? virtualDir : (virtualDir + "/");
        return path.Replace(applicationPath, virtualDir).Replace(@"\", "/");
    }

答案 2 :(得分:20)

更好的是(IMHO)实现了修复图像路径的自定义Bundle。我为我的应用写了一个。

using System;
using System.Collections.Generic;
using IO = System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Optimization;

...

public class StyleImagePathBundle : Bundle
{
    public StyleImagePathBundle(string virtualPath)
        : base(virtualPath, new IBundleTransform[1]
      {
        (IBundleTransform) new CssMinify()
      })
    {
    }

    public StyleImagePathBundle(string virtualPath, string cdnPath)
        : base(virtualPath, cdnPath, new IBundleTransform[1]
      {
        (IBundleTransform) new CssMinify()
      })
    {
    }

    public new Bundle Include(params string[] virtualPaths)
    {
        if (HttpContext.Current.IsDebuggingEnabled)
        {
            // Debugging. Bundling will not occur so act normal and no one gets hurt.
            base.Include(virtualPaths.ToArray());
            return this;
        }

        // In production mode so CSS will be bundled. Correct image paths.
        var bundlePaths = new List<string>();
        var svr = HttpContext.Current.Server;
        foreach (var path in virtualPaths)
        {
            var pattern = new Regex(@"url\s*\(\s*([""']?)([^:)]+)\1\s*\)", RegexOptions.IgnoreCase);
            var contents = IO.File.ReadAllText(svr.MapPath(path));
            if(!pattern.IsMatch(contents))
            {
                bundlePaths.Add(path);
                continue;
            }


            var bundlePath = (IO.Path.GetDirectoryName(path) ?? string.Empty).Replace(@"\", "/") + "/";
            var bundleUrlPath = VirtualPathUtility.ToAbsolute(bundlePath);
            var bundleFilePath = String.Format("{0}{1}.bundle{2}",
                                               bundlePath,
                                               IO.Path.GetFileNameWithoutExtension(path),
                                               IO.Path.GetExtension(path));
            contents = pattern.Replace(contents, "url($1" + bundleUrlPath + "$2$1)");
            IO.File.WriteAllText(svr.MapPath(bundleFilePath), contents);
            bundlePaths.Add(bundleFilePath);
        }
        base.Include(bundlePaths.ToArray());
        return this;
    }

}

要使用它,请执行:

bundles.Add(new StyleImagePathBundle("~/bundles/css").Include(
  "~/This/Is/Some/Folder/Path/layout.css"));

......而不是......

bundles.Add(new StyleBundle("~/bundles/css").Include(
  "~/This/Is/Some/Folder/Path/layout.css"));

它的作用(当不处于调试模式时)查找url(<something>)并将其替换为url(<absolute\path\to\something>)。我在10秒前写了这个东西,所以可能需要稍微调整一下。我通过确保URL路径中没有冒号(:)来考虑完全限定的URL和base64 DataURI。在我们的环境中,图像通常与其css文件位于同一文件夹中,但我已使用父文件夹(url(../someFile.png))和子文件夹(url(someFolder/someFile.png)对其进行了测试。

答案 3 :(得分:12)

没有必要指定转换或具有疯狂的子目录路径。经过多次故障排除后,我将其分离到了这个简单的&#34;规则(这是一个错误吗?)......

如果您的捆绑路径不是以所包含项目的相对根开头,则不会考虑Web应用程序根目录。

对我来说听起来更像是一个错误,但无论如何,你是如何使用当前的.NET 4.51版本修复它的。也许其他答案对于较旧的ASP.NET构建是必要的,不能说没有时间回顾性地测试所有这些。

澄清一下,这是一个例子:

我有这些文件......

~/Content/Images/Backgrounds/Some_Background_Tile.gif
~/Content/Site.css  - references the background image relatively, i.e. background: url('Images/...')

然后设置捆绑包......

BundleTable.Add(new StyleBundle("~/Bundles/Styles").Include("~/Content/Site.css"));

并将其渲染成......

@Styles.Render("~/Bundles/Styles")

并获得&#34;行为&#34; (bug),CSS文件本身具有应用程序根目录(例如&#34; http:// localhost:1234 / MySite / Content / Site.css&#34;)但是CSS图像在所有开始&#34; /内容中/Images/..."或&#34; / Images /..."取决于我是否添加转换。

甚至试图创建&#34; Bundles&#34;用于查看是否与现有路径有关的文件夹,但这并没有改变任何内容。该问题的解决方案实际上是要求捆绑包的名称必须以路径根开头。

意味着通过注册和渲染包路径来修复此示例,如..

BundleTable.Add(new StyleBundle("~/Content/StylesBundle").Include("~/Content/Site.css"));
...
@Styles.Render("~/Content/StylesBundle")

当然,你可以说这是RTFM,但我很确定我和其他人选择了这个&#34;〜/ Bundles /..."来自默认模板的路径或MSDN或ASP.NET网站上的文档中的某个路径,或者只是偶然发现它,因为实际上它是虚拟路径的一个非常合乎逻辑的名称,并且选择不与之冲突的虚拟路径是有意义的真实的目录。

无论如何,这就是它的方式。微软看不到任何错误。我不同意这一点,要么它应该按预期工作,要么抛出一些异常,或者添加一个额外的覆盖来添加捆绑路径,该路径选择是否包含应用程序根目录。我无法想象为什么有人不希望有一个应用程序根目录(通常除非您使用DNS别名/默认网站root安装您的网站)。所以实际上这应该是默认值。

答案 4 :(得分:9)

如果您引用了*.css文件,并且您在同一文件夹中有关联的*.min.css文件,我发现CssRewriteUrlTransform无法运行。

要解决此问题,请删除*.min.css文件或直接在捆绑中引用它:

bundles.Add(new Bundle("~/bundles/bootstrap")
    .Include("~/Libs/bootstrap3/css/bootstrap.min.css", new CssRewriteUrlTransform()));

在您这样做之后,您的网址将被正确转换,您的图片应该正确解析。

答案 5 :(得分:7)

尽管Chris Baxter的回答有助于解决原始问题,但在我的情况下当应用程序托管在虚拟目录时不起作用。在调查选项后,我完成了DIY解决方案。

ProperStyleBundle类包括从原始CssRewriteUrlTransform借来的代码,以正确转换虚拟目录中的相对路径。如果文件不存在,它也会抛出,并阻止对包中的文件进行重新排序(代码取自BetterStyleBundle)。

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Optimization;
using System.Linq;

namespace MyNamespace
{
    public class ProperStyleBundle : StyleBundle
    {
        public override IBundleOrderer Orderer
        {
            get { return new NonOrderingBundleOrderer(); }
            set { throw new Exception( "Unable to override Non-Ordered bundler" ); }
        }

        public ProperStyleBundle( string virtualPath ) : base( virtualPath ) {}

        public ProperStyleBundle( string virtualPath, string cdnPath ) : base( virtualPath, cdnPath ) {}

        public override Bundle Include( params string[] virtualPaths )
        {
            foreach ( var virtualPath in virtualPaths ) {
                this.Include( virtualPath );
            }
            return this;
        }

        public override Bundle Include( string virtualPath, params IItemTransform[] transforms )
        {
            var realPath = System.Web.Hosting.HostingEnvironment.MapPath( virtualPath );
            if( !File.Exists( realPath ) )
            {
                throw new FileNotFoundException( "Virtual path not found: " + virtualPath );
            }
            var trans = new List<IItemTransform>( transforms ).Union( new[] { new ProperCssRewriteUrlTransform( virtualPath ) } ).ToArray();
            return base.Include( virtualPath, trans );
        }

        // This provides files in the same order as they have been added. 
        private class NonOrderingBundleOrderer : IBundleOrderer
        {
            public IEnumerable<BundleFile> OrderFiles( BundleContext context, IEnumerable<BundleFile> files )
            {
                return files;
            }
        }

        private class ProperCssRewriteUrlTransform : IItemTransform
        {
            private readonly string _basePath;

            public ProperCssRewriteUrlTransform( string basePath )
            {
                _basePath = basePath.EndsWith( "/" ) ? basePath : VirtualPathUtility.GetDirectory( basePath );
            }

            public string Process( string includedVirtualPath, string input )
            {
                if ( includedVirtualPath == null ) {
                    throw new ArgumentNullException( "includedVirtualPath" );
                }
                return ConvertUrlsToAbsolute( _basePath, input );
            }

            private static string RebaseUrlToAbsolute( string baseUrl, string url )
            {
                if ( string.IsNullOrWhiteSpace( url )
                     || string.IsNullOrWhiteSpace( baseUrl )
                     || url.StartsWith( "/", StringComparison.OrdinalIgnoreCase )
                     || url.StartsWith( "data:", StringComparison.OrdinalIgnoreCase )
                    ) {
                    return url;
                }
                if ( !baseUrl.EndsWith( "/", StringComparison.OrdinalIgnoreCase ) ) {
                    baseUrl = baseUrl + "/";
                }
                return VirtualPathUtility.ToAbsolute( baseUrl + url );
            }

            private static string ConvertUrlsToAbsolute( string baseUrl, string content )
            {
                if ( string.IsNullOrWhiteSpace( content ) ) {
                    return content;
                }
                return new Regex( "url\\(['\"]?(?<url>[^)]+?)['\"]?\\)" )
                    .Replace( content, ( match =>
                                         "url(" + RebaseUrlToAbsolute( baseUrl, match.Groups["url"].Value ) + ")" ) );
            }
        }
    }
}

StyleBundle

一样使用它
bundles.Add( new ProperStyleBundle( "~/styles/ui" )
    .Include( "~/Content/Themes/cm_default/style.css" )
    .Include( "~/Content/themes/custom-theme/jquery-ui-1.8.23.custom.css" )
    .Include( "~/Content/DataTables-1.9.4/media/css/jquery.dataTables.css" )
    .Include( "~/Content/DataTables-1.9.4/extras/TableTools/media/css/TableTools.css" ) );

答案 6 :(得分:7)

也许我有偏见,但我非常喜欢我的解决方案,因为它没有做任何转换,正则表达式等等,并且它的代码量最少:)

适用于作为IIS网站中的虚拟目录和IIS上的根网站托管的网站

所以我创建了一个IItemTransform的Implentation封装了CssRewriteUrlTransform并使用VirtualPathUtility来修复路径并调用现有代码:

/// <summary>
/// Is a wrapper class over CssRewriteUrlTransform to fix url's in css files for sites on IIS within Virutal Directories
/// and sites at the Root level
/// </summary>
public class CssUrlTransformWrapper : IItemTransform
{
    private readonly CssRewriteUrlTransform _cssRewriteUrlTransform;

    public CssUrlTransformWrapper()
    {
        _cssRewriteUrlTransform = new CssRewriteUrlTransform();
    }

    public string Process(string includedVirtualPath, string input)
    {
        return _cssRewriteUrlTransform.Process("~" + VirtualPathUtility.ToAbsolute(includedVirtualPath), input);
    }
}


//App_Start.cs
public static void Start()
{
      BundleTable.Bundles.Add(new StyleBundle("~/bundles/fontawesome")
                         .Include("~/content/font-awesome.css", new CssUrlTransformWrapper()));
}

似乎对我有用吗?

答案 7 :(得分:6)

从v1.1.0-alpha1(预发布包)开始,框架使用VirtualPathProvider来访问文件,而不是触及物理文件系统。

更新的变压器如下所示:

public class StyleRelativePathTransform
    : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response)
    {
        Regex pattern = new Regex(@"url\s*\(\s*([""']?)([^:)]+)\1\s*\)", RegexOptions.IgnoreCase);

        response.Content = string.Empty;

        // open each of the files
        foreach (var file in response.Files)
        {
            using (var reader = new StreamReader(file.Open()))
            {
                var contents = reader.ReadToEnd();

                // apply the RegEx to the file (to change relative paths)
                var matches = pattern.Matches(contents);

                if (matches.Count > 0)
                {
                    var directoryPath = VirtualPathUtility.GetDirectory(file.VirtualPath);

                    foreach (Match match in matches)
                    {
                        // this is a path that is relative to the CSS file
                        var imageRelativePath = match.Groups[2].Value;

                        // get the image virtual path
                        var imageVirtualPath = VirtualPathUtility.Combine(directoryPath, imageRelativePath);

                        // convert the image virtual path to absolute
                        var quote = match.Groups[1].Value;
                        var replace = String.Format("url({0}{1}{0})", quote, VirtualPathUtility.ToAbsolute(imageVirtualPath));
                        contents = contents.Replace(match.Groups[0].Value, replace);
                    }

                }
                // copy the result into the response.
                response.Content = String.Format("{0}\r\n{1}", response.Content, contents);
            }
        }
    }
}

答案 8 :(得分:6)

这是一个Bundle Transform,它将用相对于该css文件的url替换css url。只需将其添加到您的捆绑包中即可解决问题。

public class CssUrlTransform: IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response) {
        Regex exp = new Regex(@"url\([^\)]+\)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
        foreach (FileInfo css in response.Files) {
            string cssAppRelativePath = css.FullName.Replace(context.HttpContext.Request.PhysicalApplicationPath, context.HttpContext.Request.ApplicationPath).Replace(Path.DirectorySeparatorChar, '/');
            string cssDir = cssAppRelativePath.Substring(0, cssAppRelativePath.LastIndexOf('/'));
            response.Content = exp.Replace(response.Content, m => TransformUrl(m, cssDir));
        }
    }


    private string TransformUrl(Match match, string cssDir) {
        string url = match.Value.Substring(4, match.Length - 5).Trim('\'', '"');

        if (url.StartsWith("http://") || url.StartsWith("data:image")) return match.Value;

        if (!url.StartsWith("/"))
            url = string.Format("{0}/{1}", cssDir, url);

        return string.Format("url({0})", url);
    }

}

答案 9 :(得分:4)

另一种选择是使用IIS URL重写模块将虚拟包图像文件夹映射到物理图像文件夹。下面是一个重写规则的示例,您可以使用它作为名为“〜/ bundles / yourpage / styles”的包 - 请注意正则表达式匹配字母数字字符以及连字符,下划线和句点,这在图像文件名中很常见

<rewrite>
  <rules>
    <rule name="Bundle Images">
      <match url="^bundles/yourpage/images/([a-zA-Z0-9\-_.]+)" />
      <action type="Rewrite" url="Content/css/jquery-ui/images/{R:1}" />
    </rule>
  </rules>
</rewrite>

这种方法会产生一些额外的开销,但允许您对包名称进行更多控制,并且还可以减少您在一个页面上引用的包的数量。当然,如果你必须引用包含相对图像路径引用的多个第三方css文件,你仍然无法绕过创建多个包。

答案 10 :(得分:4)

Grinn解决方案很棒。

但是当url中存在父文件夹相对引用时,它对我不起作用。 即url('../../images/car.png')

所以,我稍微更改了Include方法,以便为每个正则表达式匹配解析路径,允许相对路径,还可以选择将图像嵌入到css中。

我还更改了IF DEBUG以检查BundleTable.EnableOptimizations而不是HttpContext.Current.IsDebuggingEnabled

    public new Bundle Include(params string[] virtualPaths)
    {
        if (!BundleTable.EnableOptimizations)
        {
            // Debugging. Bundling will not occur so act normal and no one gets hurt. 
            base.Include(virtualPaths.ToArray());
            return this;
        }
        var bundlePaths = new List<string>();
        var server = HttpContext.Current.Server;
        var pattern = new Regex(@"url\s*\(\s*([""']?)([^:)]+)\1\s*\)", RegexOptions.IgnoreCase);
        foreach (var path in virtualPaths)
        {
            var contents = File.ReadAllText(server.MapPath(path));
            var matches = pattern.Matches(contents);
            // Ignore the file if no matches
            if (matches.Count == 0)
            {
                bundlePaths.Add(path);
                continue;
            }
            var bundlePath = (System.IO.Path.GetDirectoryName(path) ?? string.Empty).Replace(@"\", "/") + "/";
            var bundleUrlPath = VirtualPathUtility.ToAbsolute(bundlePath);
            var bundleFilePath = string.Format("{0}{1}.bundle{2}",
                                               bundlePath,
                                               System.IO.Path.GetFileNameWithoutExtension(path),
                                               System.IO.Path.GetExtension(path));
            // Transform the url (works with relative path to parent folder "../")
            contents = pattern.Replace(contents, m =>
            {
                var relativeUrl = m.Groups[2].Value;
                var urlReplace = GetUrlReplace(bundleUrlPath, relativeUrl, server);
                return string.Format("url({0}{1}{0})", m.Groups[1].Value, urlReplace);
            });
            File.WriteAllText(server.MapPath(bundleFilePath), contents);
            bundlePaths.Add(bundleFilePath);
        }
        base.Include(bundlePaths.ToArray());
        return this;
    }


    private string GetUrlReplace(string bundleUrlPath, string relativeUrl, HttpServerUtility server)
    {
        // Return the absolute uri
        Uri baseUri = new Uri("http://dummy.org");
        var absoluteUrl = new Uri(new Uri(baseUri, bundleUrlPath), relativeUrl).AbsolutePath;
        var localPath = server.MapPath(absoluteUrl);
        if (IsEmbedEnabled && File.Exists(localPath))
        {
            var fi = new FileInfo(localPath);
            if (fi.Length < 0x4000)
            {
                // Embed the image in uri
                string contentType = GetContentType(fi.Extension);
                if (null != contentType)
                {
                    var base64 = Convert.ToBase64String(File.ReadAllBytes(localPath));
                    // Return the serialized image
                    return string.Format("data:{0};base64,{1}", contentType, base64);
                }
            }
        }
        // Return the absolute uri 
        return absoluteUrl;
    }

希望它有所帮助,问候。

答案 11 :(得分:2)

您可以简单地为虚拟包路径添加另一个深度级别

    //Two levels deep bundle path so that paths are maintained after minification
    bundles.Add(new StyleBundle("~/Content/css/css").Include("~/Content/bootstrap/bootstrap.css", "~/Content/site.css"));

这是一个超级低技术的答案和一种黑客,但它有效,不需要任何预处理。鉴于其中一些答案的长度和复杂性,我更喜欢这样做。

答案 12 :(得分:2)

我遇到了这个问题,其中包含错误路径的图像和CssRewriteUrlTransform没有正确解析相对父路径..(外部资源也存在问题,如webfonts)。这就是我编写这个自定义转换的原因(似乎正确地执行了以上所有操作):

public class CssRewriteUrlTransform2 : IItemTransform
{
    public string Process(string includedVirtualPath, string input)
    {
        var pathParts = includedVirtualPath.Replace("~/", "/").Split('/');
        pathParts = pathParts.Take(pathParts.Count() - 1).ToArray();
        return Regex.Replace
        (
            input,
            @"(url\(['""]?)((?:\/??\.\.)*)(.*?)(['""]?\))",
            m => 
            {
                // Somehow assigning this to a variable is faster than directly returning the output
                var output =
                (
                    // Check if it's an aboslute url or base64
                    m.Groups[3].Value.IndexOf(':') == -1 ?
                    (
                        m.Groups[1].Value +
                        (
                            (
                                (
                                    m.Groups[2].Value.Length > 0 ||
                                    !m.Groups[3].Value.StartsWith('/')
                                )
                            ) ?
                            string.Join("/", pathParts.Take(pathParts.Count() - m.Groups[2].Value.Count(".."))) :
                            ""
                        ) +
                        (!m.Groups[3].Value.StartsWith('/') ? "/" + m.Groups[3].Value : m.Groups[3].Value) +
                        m.Groups[4].Value
                    ) :
                    m.Groups[0].Value
                );
                return output;
            }
        );
    }
}

编辑:我没有意识到,但我在代码中使用了一些自定义扩展方法。这些的源代码是:

/// <summary>
/// Based on: http://stackoverflow.com/a/11773674
/// </summary>
public static int Count(this string source, string substring)
{
    int count = 0, n = 0;

    while ((n = source.IndexOf(substring, n, StringComparison.InvariantCulture)) != -1)
    {
        n += substring.Length;
        ++count;
    }
    return count;
}

public static bool StartsWith(this string source, char value)
{
    if (source.Length == 0)
    {
        return false;
    }
    return source[0] == value;
}

当然应该可以将String.StartsWith(char)替换为String.StartsWith(string)

答案 13 :(得分:1)

经过一番调查后,我得出以下结论: 您有两个选择:

  1. 进行转换。非常实用的包:https://bundletransformer.codeplex.com/ 您需要针对每个有问题的捆绑进行以下转换:

    BundleResolver.Current = new CustomBundleResolver();
    var cssTransformer = new StyleTransformer();
    standardCssBundle.Transforms.Add(cssTransformer);
    bundles.Add(standardCssBundle);
    
  2. 优点:在此解决方案中,您可以根据需要为您的软件包命名=&gt;您可以将css文件合并到来自不同目录的一个包中。 缺点:您需要转换每个有问题的包

    1. 使用相同的相对根作为css文件所在位置的包名称。优点:无需转型。 缺点:您将来自不同目录的css表合并为一个包具有限制。

答案 14 :(得分:0)

只记得在捆绑包中修复 多个 CSS内含物,

bundles.Add(new StyleBundle("~/Content/styles/jquery-ui")
    .Include("~/Content/css/path1/somestyle1.css", "~/Content/css/path2/somestyle2.css"));

您不能仅将new CssRewriteUrlTransform()添加到一个CSS文件的末尾,因为该方法不支持该文件,因此您必须多次使用Include

bundles.Add(new StyleBundle("~/Content/styles/jquery-ui")
    .Include("~/Content/css/path1/somestyle1.css", new CssRewriteUrlTransform())
    .Include("~/Content/css/path2/somestyle2.css", new CssRewriteUrlTransform()));
相关问题