Anders Anders - 1 year ago 409
C# Question

CssRewriteUrlTransform with or without virtual directory

We are using MVC Bundling in our site,

makes sure that the image urls work from the dynamic bundle css file.

But this only works when not using a virtual directory, i.e

does not work but
does. This is because the
tranform does not take the virtual folder into account when rewriting the url. So if a image real path is
it will rewrite it to
which is wrong

Answer Source

I am not sure to fully understand your problem, but seeing http://localhost here seems wrong. You should never use an absolute URL for your bundles.

For me CssRewriteUrlTransform works perfectly, here is how I use it:

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

"Bundles" is virtual.

Does this helps?


I was confused with the "VirtualDir" thing, as you are talking about IIS VirtualDir, and I was thinking Bundle VirtualDir! It's true that in this case CssRewriteUrlTransform will rewrite URLs to the Host, not to the Host/VirtualDir URI.

To do that, you have to derive CssRewriteUrlTransform to make it do what you need it to. There is a good discussion here: ASP.NET MVC4 Bundling with Twitter Bootstrap

Seems the best answer is there:

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

Use this class instead of CssRewriteUrlTransform

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download