在静态资产和基于 CDN 的资产之间切换以进行开发和部署的最佳方式
我正在ASP.NET MVC3中进行开发和应用。我计划利用 Amazon Cloudfront 产品作为 CDN 来提供静态资产。
我很好奇是否有人设计了一种简单的方法来在用于开发的本地资产和用于部署的基于 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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
与保罗的回答类似。
过去,我使用了 UrlHelper 的扩展方法,该方法根据 web.config 中的值创建链接。
这很有帮助,因此您不必在发布后缩小视图,并且就像在发布时更新 web.config 条目一样简单。任何需要使用 CDN 资源的资源,您只需说
Url.CdnContent("~/site.css")
我目前不在我的开发电脑上,但是当我回到家时,我将为您提供我的扩展方法的源代码
它非常简单,但它适用于我需要它做的事情
Similarly to Paul's answer.
In the past I've used an extension method for UrlHelper that created the links based on a value from the web.config.
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")
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
我过去使用一些简单的规则做到了这一点:
Url.Content
的应用程序相对路径,即Url.Content("~/content/file.jpg")
)然后在我的部署过程中,我可以简单地将所有静态资源从站点复制到 CDN,CSS 将正常工作,因为它的相对值(CSS
url()
值始终相对于它们所在的 CSS 文件,而不是请求),并且我将使用正则表达式来替换我的视图中的任何字符串,这些字符串的形式是我期望具有 CDN 基本路径的形式。I've done it in the past using a few simple rules:
Url.Content
, ie.Url.Content("~/content/file.jpg")
)Then in my deploy process I can simply copy all static assets from the site to the CDN, the CSS will just work since its relative (CSS
url()
values are always relative to the CSS file they are in, not the request), and I will use regex to replace any strings in my views that are in the form I expect to have the CDN base path.好问题。我建议您使用条件编译变量。
如果您的项目处于调试模式,则会链接本地资源。如果您的项目处于发布模式,CDN 资产将被链接。
这是一个示例:
但是请注意,当您发布项目时,它应该处于发布模式。有一次,我更新了我的一个项目,它处于调试模式,但一切都出了问题。
以下是一些关于条件编译的好链接:
http://haacked.com/archive/2007/09/16/conditional-compilation-constants-and-asp.net.aspx
http://odetocode .com/blogs/scott/archive/2005/12/01/conditional-compilation-in-asp-net-2-0.aspx
http://odetocode .com/blogs/scott/archive/2007/09/24/more-on-conditional-compilation-in-asp-net.aspx
Nice question. I suggest you use Conditional Compilation Variables.
If your project is in debug mode, local assets would be linked. If your project is in release mode, CDN assets would be linked.
Here is a sample:
But be careful, when you publish you project, it should be in release mode. Once, I updated one of my projects and it was in DEBUG mode and everything went wrong.
Here are some nice links about Conditional Compilation:
http://haacked.com/archive/2007/09/16/conditional-compilation-constants-and-asp.net.aspx
http://odetocode.com/blogs/scott/archive/2005/12/01/conditional-compilation-in-asp-net-2-0.aspx
http://odetocode.com/blogs/scott/archive/2007/09/24/more-on-conditional-compilation-in-asp-net.aspx
我有一组使用的扩展方法(见下文)。您可以使用它们作为基础/示例来创建您自己的自定义调试/发布扩展方法。
常规调试/发布:
常规调试/发布用法:
调试/发布 CSS 标签:
调试/发布 CSS 标签用法:
调试/发布 JS 标签:
调试/发布 JS 标签用法:
附加调试/发布选项:
附加调试/发布选项用法:
I have a set of extension methods I use (see below). You can use these as a base/example to create your own custom debug/release extension methods.
General debug/release:
General debug/release usage:
Debug/release CSS tags:
Debug/release CSS tags usage:
Debug/release JS tags:
Debug/release JS tags usage:
Additional debug/release options:
Additional debug/release options usage:
我专门开发了一个库来解决这个问题。
https://github.com/vincpa/mvc.resourceloader
I've developed a library specifically to tackle this problem.
https://github.com/vincpa/mvc.resourceloader