Things to know for hosting your ASP.NET MVC app
When you are planning to host your asp.net MVC application under IIS7 integrated mode environment. There are few easy tweaks that can make your application or starter kit work right out of the box but also can save a lot of your time finding things out.
You might see a Http 404 page if default document is not configured properly. One way of getting this around by using the following in global.asax during the App_BeginRequest
if (Context.Request.FilePath == "/") Context.RewritePath("Default.aspx");
But rater doing code changes, another easy way of achieving it by adding the following snippet in your web.config
<add value="Default.aspx" />
Relative urls like one that is shown below
<img alt="Flickr Logo" src="/Views/Shared/images/flickr_logo.gif"" />
This will result in invalid output when the app is hosted under IIS7 and if it is hosted under a virtual directory for example, http://localhost/flickrweb. It will produce url http://localhost/Views/Shared/images/flickr_logo.gif rather the original http://localhost/ flickrweb/Views/Shared/images/flickr_logo.gif. To overcome this we would need to use Page.ResolveClientUrl which makes the above snippet look like
<img alt="Flickr Logo"
src="<%= Page.ResolveClientUrl("~/Views/Shared/images/flickr_logo.gif") %>"/>
Now, while hosting our MVC app to a shared hosting server whether or not they provide us a wwwlogs folder, we can easily write down a custom HttpLog module for IIS7 integrated mode (blessing of integrated pipeline). On the Init method of the HttpModule we can add the following event mapping under which we can write our custom logging logic.
context.LogRequest +=new EventHandler(context_LogRequest);
For, each http request this will be fired for IIS7 integrated mode and we need to add the mapping for it in the following way.
<add name="<ModuleName>" preCondition="integratedMode"
We might be using CSS handlers that will be compressing the contents and combine cross browser CSS intelligently, like one I have posted few months back
Make sure you have added the mapping for this as well properly under system.webserver node for integrated mode
<add name="<handlerName>" path="<path>.axd" verb="*"
requireAccess="Script" preCondition="integratedMode" />
And of course add it with Page.ResolveClient url to make it an absolute work.
href="<%= Page.ResolveClientUrl("~/CSSHander.axd") %>" type="text/css" />
Now, hosting extension less web app is all beauty but how if someone tries to overload your server by manually hitting /Views/Controller/SomePage.aspx. We can make sure of it that no .aspx file is ever processed by manual hits, by putting an extra web.config file under /Views folder of you ASP.NET MVC application and adding this tiny line inside it
<add name="InvalidAspxHandler" path="*.aspx" verb="*" preCondition="integratedMode"
type="System.Web.HttpNotFoundHandler, System.Web, Version=18.104.22.168, Culture=neutral
Now, all the config changes I have shown here are to be added under system.webserver node to work with IIS7 integrated mode. The typical webserver node for IIS7 integrated mode looks like
<modules runAllManagedModulesForAllRequests="true" />