【发布时间】:2012-08-16 09:25:11
【问题描述】:
我在 Microsoft.AspNet.Web.Optimization 命名空间(例如 @Styles.Render("~/content/static/css"))中使用 ASP.NET MVC 4 捆绑和缩小功能。
我想将它与 Windows Azure CDN 结合使用。
我考虑编写自定义 BundleTransform,但内容尚未优化。
我还研究了在运行时解析和上传优化的流,但这对我来说就像是 hack,我不太喜欢它:
@StylesCdn.Render(Url.AbsoluteContent(
Styles.Url("~/content/static/css").ToString()
));
public static IHtmlString Render(string absolutePath)
{
// get the version hash
string versionHash = HttpUtility.ParseQueryString(
new Uri(absolutePath).Query
).Get("v");
// only parse and upload to CDN if version hash is different
if (versionHash != _versionHash)
{
_versionHash = versionHash;
WebClient client = new WebClient();
Stream stream = client.OpenRead(absolutePath);
UploadStreamToAzureCdn(stream);
}
var styleSheetLink = String.Format(
"<link href=\"{0}://{1}/{2}/{3}?v={4}\" rel=\"stylesheet\" type=\"text/css\" />",
cdnEndpointProtocol, cdnEndpointUrl, cdnContainer, cdnCssFileName, versionHash
);
return new HtmlString(styleSheetLink);
}
如何将捆绑和缩小的版本自动上传到我的 Windows Azure CDN?
【问题讨论】:
-
Nate Totten 做了这样的事情:github.com/ntotten/wa-cdnhelpers/wiki。不过,请访问该存储库的主页……看起来他最近推荐了其他解决方案。
-
你能告诉我这个 _versionHash 参数在哪里吗?谢谢。
-
@BarbarosAlp
_versionHash表示添加到您的资产中的查询字符串v。在上面的实现中,它将与之前缓存的字符串进行比较。 -
快3年了,有没有人找到OTB解决方案?
标签: azure asp.net-mvc-4 cdn bundling-and-minification asp.net-optimization