24Nov/091
Adding CDN Functionality to ASP.net MVC
I bet everyone develop webpage today are most likely to use a CDN for deliver images, video. Unlike in rails, MVC does not have a way to link with CDN, so i write this extension for your HtmlHelper so that you can easily link to a CDN that preconfig in your Web.config
First, we need to add the ff. to web.config
<appsettings> <add key="cdn" value="cdn.ducdigital.com, cn.ducdigital.com" /> </appsettings>
and then use the ff class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static class CDNHelperExtension { public static string CDNLink(this HtmlHelper html, string path) { string[] CDN = ConfigurationSettings.AppSettings["cdn"].Replace(" ", "").Split(','); Random r = new Random(); return CDNLink(html, path, r.Next(0, CDN.Count())); } public static string CDNLink(this HtmlHelper html, string path, int ServerNumber) { string[] CDN = ConfigurationSettings.AppSettings["cdn"].Replace(" ", "").Split(','); if (ServerNumber >= CDN.Count()) ServerNumber = 0; return "http://" + CDN[ServerNumber] + path; } } |
in your View, just call:
< %= Html.CDNLink("/images/") %>
Good luck!