如何维护开发站点和生产站点的 CDN 镜像链接?
我有一个 ASP.NET MVC 应用程序,现在我想将其图像链接分离到 CDN。 目前,图像通常具有以下标记代码:
<%= Html.ActionLinkWithImage("/Content/Img/Image1.png" ...
这在使用本地 Web 服务器 (Cassini) 的开发中以及在发布产品服务器时效果很好。
但现在我希望生产 URL 类似于:
http://cdn.com/Img/Image1.png
如何让它以一种简单的方式工作,以便它在开发中以及发布到产品后无缝工作?
I have this ASP.NET MVC application that I now want to separate out the image links to a CDN.
At this moment, images typically have this markup code:
<%= Html.ActionLinkWithImage("/Content/Img/Image1.png" ...
This works fine in development using the local web server (Cassini), and also when published the prod server.
But now I want the production URL to something like:
http://cdn.com/Img/Image1.png
How do I get this to work in an easy way, so that it works seamlessly in dev and after published to prod?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个 URL 扩展方法,该方法将根据 Web.config 文件(或其他文件)中的值生成调试 URL 或实时 URL。
然后,您可以在需要图像 url 的任何地方使用此扩展方法(包括 Html 扩展方法):
确保 Url 扩展方法所在的命名空间具有
using
语句,该语句位于文件顶部你在哪里声明了 HTML 扩展方法,否则它不会识别它。You can create a URL extension method that will, depending on a value from the Web.config file (or something else), generate the debug URL or the live URL.
You can then use this extension method anywhere you need an image url (including Html extension methods):
Make sure you have the
using
statement for the namespace where the Url extension method is located at the top of the file where you declared the HTML extension method, otherwise it will not recognize it.您还可以修改您的主机文件,将您的 CDN 域重定向到另一个 IP 地址。我一直用它来将 static.myresources.com 重定向到本地主机,并且它可以使您的 html 保持非常干净。
You can also modify your host file to redirect your cdn domain to another IP address. I use this to redirect static.myresources.com to localhost all the time and it keeps your html pretty clean.