How to use CDN links in your ASP.NET MVC application to serve static content from a cookieless domain

Here’s one way you can use CDN links in your ASP.NET MVC application.

Problem:
When you develop your ASP.NET MVC application and use a link to a static content, such as css, jpg’s etc, you may want to server these from a CDN or at least a domain that doesn’t server cookies with the request. For those of you who have been using the Google Page Speed add-in for FireFox, you know what I’m talking about:

Untitled1

So the question is, how do you get your markup that normally may look something like this:


to look something like this on your production server:


When you are developing using the local web server (Cassini), you don’t want the CDN links in your code, but after you have published the page to prod, you want it to use the CDN link. One way to make this work is to make an extension helper method that creates the correct link based on if you run in dev or prod. Here is my suggestion:

public static MvcHtmlString CDNLink(this HtmlHelper html,
string tagName,
string contentTag,
string contentUrl,
object htmlAttributes)
{
var contentLink = new TagBuilder(tagName);
var contentServerUrl = ConfigurationManager.AppSettings["ContentServerUrl"];
contentServerUrl = string.IsNullOrEmpty(contentServerUrl) ? "{0}" : contentServerUrl;
contentLink.MergeAttribute(contentTag, string.Format(contentServerUrl, contentUrl));
contentLink.MergeAttributes((IDictionary<string, string="">)htmlAttributes, true);
return MvcHtmlString.Create(contentLink.ToString());
}

 

It is pretty straightforward to use:

<%=html.cdnlink("link", new="" string="">
{
{"rel", "stylesheet"},
{"type","text/css"}
}) %>

<%=html.cdnlink(“link”, new=”” string=””>

<%=html.cdnlink(“link”, new=”” string=””>

Pass the tag name (“link”), the content attribute (“href”), the actual content link (“file.css”) and optional HTML attributes. In your web.config file, add an application settings for the content server url like this in dev


And in prod, use something like this:

There you go, hope you will find it useful.

 

 

But once in a while he chokes. And then the brother actually has to pad him on the back to get it cleaned up

 

Quote of the day, on Intel i7 series of processors with 6 cores and hyperthreading:
 
"You have six with six brain damaged cousins. 
And sometimes you can hand a cousine: here eat this while I'm doing the real work, and everything is fine.
But once in a while he chokes. And then the brother actually has to pad him on the back to get it cleaned up."
 
Richard Campbell, in DotNetRocks episode 582, at about 48:44
 
To get some more wisdom from Mr Campbell, listen to the excellent podcasts RunAsRadio and DotNetRocks

How to install IIs 7.5 on Windows 7

This post will guide you through the steps on how to install IIs 7.5 on Windows 7. You must have administrator privileges to perform this operation.

1. Go to Control Panel –> Programs and Features –> Turn Windows features on or off.

Control Panel, Programs and Features, Turn Windows features on or off

2. Put a checkmark in the World Wide Web Services. This will select the most common features for the web server. In my case, I also have selected the ASP.NET, and Basic and Windows Authentication features. Some features may not be available depending on which edition of Windows 7 you have. This article will give an overview on Available Web Server (IIS) Role Services in IIS 7.5 for the various editions.

Turn Windows features on and off, select World Wide Web Services

3. Click Ok and Windows will install the selected features.

Windows makes changes to features.

4. Verify that the installation succeeded by browsing to http://localhost. This should display the IIs logo.

Showing the IIS logo

5. If you have done this after installation Visual Studio 2010 (and .NET Framework v4) you will only have the default .NET v2 application pools:

.NET Framework v2 application pools

so in order to get the .NET Framework v4 integration and application pools, you will have to register the .NET Framework v4 with IIS. Open an elevated command prompt change directory to C:\Windows\Microsoft.NET\Framework\v4.0.30319

and type: aspnet_regiis.exe -i

Now, the .NET Framework v4 application pools are available:

.NET Framework v4 application pools