我正在 ASP.NET MVC3 中开发和应用程序。我计划利用 Amazon 的 Cloudfront 产品作为 CDN 来提供静态 Assets 。
我很好奇是否有人设计了一种在用于开发的本地 Assets 和用于部署的基于 CDN 的 Assets 之间切换的简单方法?
任何提示或技巧将不胜感激。
最佳答案
类似于保罗的回答。
过去,我为 UrlHelper 使用了一种扩展方法,该方法基于 web.config 中的值创建链接。
这很有用,因此您不必在发布后最小化 View ,而且它就像在发布时更新 web.config 条目一样简单。任何需要使用 CDN 资源的资源,你只需说 Url.CdnContent("~/site.css")
我现在不在我的开发电脑上,但当我回到家时,我会给你我的扩展方法的源代码
它非常简单,但它可以满足我需要它做的事情
public static string CdnContent(this UrlHelper helper, string relativePath)
{
var cdnRoot = ConfigurationManager.AppSettings["cygnus.cdnroot"];
if (string.IsNullOrEmpty(cdnRoot))
return UrlHelper.GenerateContentUrl(relativePath, helper.RequestContext.HttpContext);
if (relativePath.StartsWith("~"))
relativePath = relativePath.Substring(1);
if (cdnRoot.EndsWith("/"))
cdnRoot = cdnRoot.Substring(0, cdnRoot.Length - 1);
if (!relativePath.StartsWith("/"))
relativePath = "/" + relativePath;
return cdnRoot + relativePath;
}
关于c# - 在静态 Assets 和基于 CDN 的 Assets 之间切换以进行开发和部署的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9365191/