2

For an MVC 4 project, I have a requirement to include/bundle content files that reside outside the MVC project e.g. somewhere in a CDN.

This works:

bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/Css/site.css"));

This does not work:

bundles.Add(newStyleBundle("~/Content/css").Include("D:/Media/Styles/Css/site.css"));

The error I get is:

The URL 'D:/Media/Styles/Css/site.css' is not valid. Only application relative URLs (~/url) are allowed. Parameter name: virtualPaths

In short, is it at all possible to have asset files residing outside the project's Content folder, for example when using a CDN? if so, what is the best approach?

1 Answer 1

3

You can only use relative paths and CDN. The resources can be outside the Content folder, so you can have them in a different folder within your site.

You can also use a CDN like this:

public static void RegisterBundles(BundleCollection bundles)
{

    bundles.UseCdn = true;   //enable CDN support

    //add link to jquery on the CDN
    var jqueryCdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js";

    bundles.Add(new ScriptBundle("~/bundles/jquery",
                jqueryCdnPath).Include(
                "~/Scripts/jquery-{version}.js"));

    // Code removed for clarity.
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm having trouble finding much documentation on this aside from the link you included. Would this practice be the same for a new StyleBundle CDN?
Just achiueved this exact behavior, StyleBundles can be implemented in the same was from a CDN.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.