Put images on a CDN using MVC3 on IIS7

I need to use CDN for all images on my site. So, Ive decided to use the IIS Url rewrite module, because manually editing all my site views is impossible for me.

So, Ive created the rules for IIS, for example:

<rule name="cdn1" stopProcessing="true"> <match url="^Content/Images.*/(.*\.(png|jpeg|jpg|gif))$" /> <action type="Redirect" url="http://c200001.r9.cf1.rackcdn.com/{ToLower:{R:1}}" redirectType="Permanent" /> </rule> 

It worked, but as you can see, the redirect type is used (301 Permanent). And I think that this affects the performance of the site. Perhaps you can modify Request.Output to replace the image URL?

Please advice, how can I use CDN for images, not edit my views and avoid redirection?

Any help would be appreciated

+8
url-rewriting iis-7 asp.net-mvc-3 cdn url-rewrite-module
source share
2 answers

I agree with Steve. You have a URL relay that performs 301 redirects, but for each image that the page requires, the browser still first contacts the server to find that it is 301 redirected to the CDN address. The only thing you save at this point is loading the content.

Instead, you can simply put the response filter in place to change the URLs of these assets before sending the response to client-client. Thus, the client browser should never make any calls to your server for static assets:

 protected override void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.RequestContext.HttpContext.Response.Filter = new CdnResponseFilter(filterContext.RequestContext.HttpContext.Response.Filter); } 

And then CdnResponseFilter:

 public class CdnResponseFilter : MemoryStream { private Stream Stream { get; set; } public CdnResponseFilter(Stream stream) { Stream = stream; } public override void Write(byte[] buffer, int offset, int count) { var data = new byte[count]; Buffer.BlockCopy(buffer, offset, data, 0, count); string html = Encoding.Default.GetString(buffer); html = Regex.Replace(html, "src=\"/Content/([^\"]+)\"", FixUrl, RegexOptions.IgnoreCase); html = Regex.Replace(html, "href=\"/Content/([^\"]+)\"", FixUrl, RegexOptions.IgnoreCase); byte[] outData = Encoding.Default.GetBytes(html); Stream.Write(outData, 0, outData.GetLength(0)); } private static string FixUrl(Match match) { //However the Url should be replaced } } 

As a result, all content assets that look like <img src="\Content\whatever.jpg" /> will be converted to <img src="cdn-url.com\Content\whatever.jpg" />

+7
source share

Given that the source URLs come from your content, and not, for example, from bookmarks, I think that you will not be able to avoid a request to your site and redirects; a potentially significant performance impact that may negate the benefits of using CDNs.

It would be better if you could apply URL rewriting in HTML, which is passed to the browser, and not when the request arrives.

The problem is that I do not know how (if you are not using the ISA server, in which case I could tell you, but I suspect you did not!)

You can create your own ActionFilter and override OnResultExecuted, but you will need to annotate your controller with a filter attribute.

+1
source share

All Articles