在静态资产和基于CDN的资产之间进行开发和部署的最佳方式 [英] Best way to switch between static assets and CDN based assets for development and deployment

查看:60
本文介绍了在静态资产和基于CDN的资产之间进行开发和部署的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ASP.NET MVC3中进行开发和应用.我打算利用Amazon Cloudfront产品作为CDN来服务静态资产.

I am developing and application in ASP.NET MVC3. I am planning to take advantage of Amazons Cloudfront offering as a CDN to serve static assets.

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

I am curious if anyone has devised a simple method for switching between local assets for development and CDN based assets for deployment?

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

Any tips or tricks would be greatly appreciated.

推荐答案

与保罗的回答类似.

过去,我为UrlHelper使用了扩展方法,该方法基于web.config中的值创建了链接.

In the past I've used an extension method for UrlHelper that created the links based on a value from the web.config.

这很有用,因此您不必在发布后最小化视图,就像在发布时更新web.config条目一样简单.任何需要使用CDN资源的资源,您只需说Url.CdnContent("~/site.css")

This is helpful so you don't have to minipulate your views after publishing, and it's as simple as updating a web.config entry on publish. Any resources that require using the CDN resource, you simply say Url.CdnContent("~/site.css")

目前我不在开发PC上,但是当我回到家时,我会为您提供扩展方法的源代码

I'm not on my development pc at the moment, but when I get home, I'll get you the source for my extension method

这非常简单,但是它可以满足我的需要

It's very simplistic, but it works for what I need it to do

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;
}

这篇关于在静态资产和基于CDN的资产之间进行开发和部署的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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