Search code examples
c#asp.net-mvcmodel-view-controlleriis-5

ASP.NET MVC in a virtual directory


I have the following in my Global.asax.cs

routes.MapRoute(
    "Arrival",
    "{partnerID}",
    new { controller = "Search", action = "Index", partnerID="1000" }
);

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = "" }
);

My SearchController looks like this

public class SearchController : Controller
{
    // Display search results
    public ActionResult Index(int partnerID)
    {
        ViewData["partnerID"] = partnerID;
        return View();
    }
}

and Index.aspx simply shows ViewData["partnerID"] at the moment.

I have a virtual directory set up in IIS on Windows XP called Test.

If I point my browser at http://localhost/Test/ then I get 1000 displayed as expected. However, if I try http://localhost/Test/1000 I get a page not found error. Any ideas?

Are there any special considerations for running MVC in a virtual directory?


Solution

  • IIS 5.1 interprets your url such that its looking for a folder named 1000 under the folder named Test. Why is that so?

    This happens because IIS 6 only invokes ASP.NET when it sees a “filename extension” in the URL that’s mapped to aspnet_isapi.dll (which is a C/C++ ISAPI filter responsible for invoking ASP.NET). Since routing is a .NET IHttpModule called UrlRoutingModule, it doesn’t get invoked unless ASP.NET itself gets invoked, which only happens when aspnet_isapi.dll gets invoked, which only happens when there’s a .aspx in the URL. So, no .aspx, no UrlRoutingModule, hence the 404.

    Easiest solution is:

    If you don’t mind having .aspx in your URLs, just go through your routing config, adding .aspx before a forward-slash in each pattern. For example, use {controller}.aspx/{action}/{id} or myapp.aspx/{controller}/{action}/{id}. Don’t put .aspx inside the curly-bracket parameter names, or into the ‘default’ values, because it isn’t really part of the controller name - it’s just in the URL to satisfy IIS.

    Source: http://blog.codeville.net/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/