MVC 4 Bundle - unable to bundle content e.g. css/javascript outside MVC project content folder

送分小仙女□ 提交于 2019-12-24 12:44:15

问题


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:


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.
}


来源:https://stackoverflow.com/questions/14375735/mvc-4-bundle-unable-to-bundle-content-e-g-css-javascript-outside-mvc-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!