Sunday, 18 December 2011

Fixing MVC in a virtual directory under IIS6

MVC sites gets broken when you run them in a virtual directory in IIS6. 

Specifically, the routing stops working and the paths to any images, css and javascript files gets broken

Here's what you need to know to fix them:

1) Routing
In IIS6, open the properties dialog and from the Virtual Directory tab, setup wildcard mappings to handle MVC's dimensionless URLS:
• click Configuration
• click Insert
• enter path to the aspnet_isapi.dll file for version of aspnet you are using, e.g. for .net 4 aspnet_isapi.dll (in C:\Windows\Microsoft.NET\Framework\v4.0.30319)
• untick Verify that file exists (this is important!)


You can read more about this on Stephen Sanderson's blog.

2) Image and js paths

To deploy under a Virtual Directory:
In Site.Master
css hrefs need ~ prefix in path eg
<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />

script source refs fixed with the following

<% var siteroot = Url.Content("~/"); %>
<script src="<%: siteroot %>Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>

image paths fixed by rewriting value for src in the image tag as, eg

src="/Content/myimage.png"

rewrite as

src="<%=Url.Content("~/Content/myimage.png") %>"

and note ~ prefix in path