如何将捆绑和缩小的文件上传到 Windows Azure CDN [英] How to upload bundled and minified files to Windows Azure CDN

查看:18
本文介绍了如何将捆绑和缩小的文件上传到 Windows Azure CDN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 中使用 ASP.NET MVC 4 捆绑和缩小功能Microsoft.AspNet.Web.Optimization 命名空间(例如 @Styles.Render("~/content/static/css")).

我想将它与 Windows Azure CDN 结合使用.

I'd like to use it in combination with a Windows Azure CDN.

我考虑编写自定义BundleTransform,但内容尚未优化.

I looked into writing a custom BundleTransform but the content is not optimized there yet.

我还研究了在运行时解析和上传优化的流,但这对我来说感觉像是一种黑客行为,我真的不喜欢它:

I also looked into parsing and uploading the optimized stream on runtime but that feels like a hack to me and I don't really like it:

@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?

How can I upload the bundled and minified versions automatically to my Windows Azure CDN?

推荐答案

所以目前没有很好的方法来做到这一点.我们设想的长期工作流程是添加构建时捆绑支持.然后,您将运行构建任务(或运行 exe,如果您愿意)来生成包,然后能够将它们上传到 AzureCDN.最后,您只需在 BundleCollection 上打开 UseCDN,脚本/样式助手就会自动切换到呈现指向您的 AzureCDN 的链接,并适当回退到您的本地包.

So there isn't a great way to do this currently. The longer term workflow we are envisioning is adding build-time bundling support. Then you would run a build task (or run an exe if you prefer) to generate the bundles and then be able to upload these to the AzureCDN. Finally, you just turn on UseCDN on the BundleCollection, and the Script/Style helpers would just automatically switch to rendering out links to your AzureCDN with proper fallback to your local bundles.

就短期而言,我认为您尝试做的是在首次构建捆绑包时将捆绑包上传到 AzureCDN?

For the short term, what I think you are trying to do is upload your bundle to the AzureCDN when the bundle is first constructed?

我猜BundleTransform 是一种方法,它有点小技巧,但您可以在您的bundle 中最后添加一个BundleTransform.自上次以来, BundleResponse.Content 实际上是最终的捆绑响应.届时,您可以将其上传到 CDN.有意义吗?

A BundleTransform is one way to do it I guess, its a bit of a hack, but you could add a BundleTransform last in your bundle. Since its last, the BundleResponse.Content is effectively the final bundle response. At that point in time you can upload it to your CDN. Does that make sense?

这篇关于如何将捆绑和缩小的文件上传到 Windows Azure CDN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆