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?

rekire
  • 47,260
  • 30
  • 167
  • 264
Girma
  • 21
  • 1
  • 2

1 Answers1

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.
}
robasta
  • 4,621
  • 5
  • 35
  • 53
  • 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? – WiseGuy Aug 06 '13 at 15:10
  • Just achiueved this exact behavior, StyleBundles can be implemented in the same was from a CDN. – WiseGuy Aug 08 '13 at 14:37