这就是我所做的——至少现在是这样:
搜索和替换:
html.image("~
为
Html.CDNImage("~
然后在静态类ImageExtensions 中创建了一个助手:
public static string CDNImage(this HtmlHelper htmlHelper, string imageRelativeUrl)
{
return CDNImage(htmlHelper, imageRelativeUrl, null, null);
}
public static string CDNImage(this HtmlHelper htmlHelper, string imageRelativeUrl, object htmlAttributes)
{
return CDNImage(htmlHelper, imageRelativeUrl, null, htmlAttributes);
}
public static string CDNImage(this HtmlHelper htmlHelper, string imageRelativeUrl, string alt, object htmlAttributes)
{
string url = Regex.Replace(imageRelativeUrl, "~/content/", "http://s3.amazon.com/", RegexOptions.Compiled | RegexOptions.IgnoreCase);
return htmlHelper.Image(url, alt, htmlAttributes);
}
显然,我在这里使用s3.amazon.com 作为占位符 - 无论是什么内容,您都必须填写 CDN 的 URL。
如果需要,您可以使用某种配置属性来确定您是否实际进行替换。