使用内置的MVC4打捆,我怎么在前面加上我的CDN网址到它产生的链接标签? 我设置亚马逊的Cloudfront,这样就会从我的web服务器资产第一次请求时。 所以,当我定义了一个包,如下所示:
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/reset.css",
"~/Content/960_24_col.css",
"~/Content/Site.css"
));
在部署时,我可以这样引用它:
http://[cloundfrontid].cloudfront.net/Content/css?v=muhFMZ4thy_XV3dMI2kPt-8Rljm5PNW0tHeDkvenT0g1
现在我只需要改变由所述捆绑件相对于指着我的CDN绝对链接产生的链接。
<link href="[INSERT_CDN_URL_HERE]/Content/css?v=muhFMZ4thy_XV3dMI2kPt-8Rljm5PNW0tHeDkvenT0g1" rel="stylesheet"/>
我认为有可能改写使用IBundleTransform的路径,但我找不到任何的例子。
注:只是要清楚,我知道你可以指定一个CDN链接,捆绑,但只有当束可以通过静态链接来代替工作。
我刚刚安装MaxCDN跑进完全相同的问题。
如你所知,在bundles.UseCdn
因为我们不希望有指定包的确切url属性是不理想的。 像Max CDN A CDN是完全相同的网址,查询字符串,所有的,除了一个不同的子域。
这里是我结束了解决它。
我创建了一个BundleHelper
类,将包裹渲染方法,然后在前面加上与CDN子域的路径。
这里是什么类的样子:
namespace MyDomain.Web.Helpers
{
public class BundleHelper
{
public static string CdnPath = "http://cdn.mydomain.com";
public static IHtmlString RenderScript(string path)
{
var opt = System.Web.Optimization.Scripts.Render(path);
string htmlString = HttpUtility.HtmlDecode(opt.ToHtmlString());
if (BundleTable.EnableOptimizations)
{
htmlString = htmlString.Replace("<script src=\"/", String.Format("<script src=\"{0}/", CdnPath));
}
return new HtmlString(htmlString);
}
public static IHtmlString RenderStyle(string path)
{
var opt = System.Web.Optimization.Styles.Render(path);
string htmlString = HttpUtility.HtmlDecode(opt.ToHtmlString());
if (BundleTable.EnableOptimizations)
{
htmlString = htmlString.Replace("<link href=\"/", String.Format("<link href=\"{0}/", CdnPath));
}
return new HtmlString(htmlString);
}
}
}
然后在视图中使用它,我只是做:
@BundleHelper.RenderStyle("~/Content/css")
@BundleHelper.RenderStyle("~/Content/themes/base/css")
@BundleHelper.RenderScript("~/bundles/jquery")
@BundleHelper.RenderScript("~/bundles/jqueryui")
希望这可以帮助。
Please have a look @ Using a CDN search for "Using a CDN"
As said by By Rick Anderson in asp.net/mvc,
The follow code replaces the local jQuery bundle with a CDN jQuery
bundle.
public static void RegisterBundles(BundleCollection bundles)
{
//bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
// "~/Scripts/jquery-{version}.js"));
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.
}
In the code above, jQuery will be requested from the CDN while in
release mode and the debug version of jQuery will be fetched locally
in debug mode. When using a CDN, you should have a fallback mechanism
in case the CDN request fails. The following markup fragment from the
end of the layout file shows script added to request jQuery should the
CDN fail.
</footer>
@Scripts.Render("~/bundles/jquery")
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
var e = document.createElement('script');
e.src = '@Url.Content("~/Scripts/jquery-1.7.1.js")';
e.type = 'text/javascript';
document.getElementsByTagName("head")[0].appendChild(e);
}
</script>
@RenderSection("scripts", required: false)
</body>
</html>
In have pasted the section from Asp.net/MVC, in case you find it useful then Cheers to Rick Anderson for his post...
看看我的回答类似的问题在这里: https://stackoverflow.com/a/18500359/725626
建立在BigJoe714答案,并提出了一个稍微不同的选择。