在静态资产和基于CDN的资产之间切换以进行开发和部署的最佳方法

ste*_*776 7 .net c# asp.net asp.net-mvc asp.net-mvc-3

我正在ASP.NET MVC3中开发和应用程序.我计划利用亚马逊Cloudfront产品作为CDN来提供静态资产.

我很好奇是否有人设计了一种简单的方法来切换用于开发的本地资产和用于部署的基于CDN的资产?

任何提示或技巧将不胜感激.

Ant*_*haw 7

与保罗的回答相似.

在过去,我使用了UrlHelper的扩展方法,该方法根据web.config中的值创建链接.

这很有用,因此您不必在发布后轻松查看视图,这就像在发布时更新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;
}
Run Code Online (Sandbox Code Playgroud)