Running a ASP.NET MVC Beta site on IIS 5.1 and 6
Background
During the development of site on ASP.NET MVC I have faced some issues, which is related to IIS (6 and 5.1). I have Windows XP on my development box and Windows 2003 on my production server. I want to use my local IIS during the development and also want to deploy my site on remote server without any modification in codes.
Issue
If we create a new project in VS 2008 with ASP.NET MVC Beta and host this template site on IIS (6 or 5.1) then after opening site in browser will get following output.
Issue 1: Design is not applied on root (home) page
Home page does not use css and other formatting, Images. Home page will become:
Issue 2: Call to controller fails and "The page cannot be found" error thrown by IIS.
And if we click on any link, which is actually a call of controller the “The page cannot be found” error thrown by IIS.
Here is the solution for this issue (http://go.microsoft.com/?LinkId=9394801) One method is suggested to append “.aspx” in controller. Also other methods are provided there but I am not interested in them. Adding “.aspx” in controller will resolve the problem but it will not handle root url issue (Issue 1).
Resolution
Resolution 1:
Open Default.aspx.cs of root folder in your project. Modify the Page_Load function as per given below. Added codes are formatted Bold.
public void Page_Load(object sender, System.EventArgs e)
{
if (Request.ApplicationPath == "")
{
HttpContext.Current.RewritePath(Request.ApplicationPath + "home.aspx");
}
else if (Request.ApplicationPath == HostingEnvironment.ApplicationVirtualPath)
{
HttpContext.Current.RewritePath(Request.ApplicationPath + "/home.aspx");
}
else
{
HttpContext.Current.RewritePath(Request.ApplicationPath);
}
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
}
Now application must be working fine on IIS 5.1 and IIS 6 without facing issue 1.
Resolution 2:
To resolve issue# 2, we need to open Global.aspx.cs file and modify RegisterRoutes function. Added codes are formatted Bold.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Default", "{controller}.aspx/{action}/{id}",
new { controller = "Home", action = "Index", id = "" });
}
Now application must be working fine on IIS 5.1 and IIS 6 without facing issue 2. But addition of ".aspx" in controller will be displayed in url.
Thanks and Regards,
Ajit