Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 817
Evil Andy Avatar asked Oct 08 '08 14:10

Evil Andy


2 Answers

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/

like image 59
Amith George Avatar answered Oct 06 '22 00:10

Amith George


If you are doing this on Windows XP, then you're using IIS 5.1. You need to get ASP.Net to handle your request. You need to either add an extension to your routes ({controller}.mvc/{action}/{id}) and map that extension to ASP.Net or map all requests to ASP.Net. The http://localhost/Test works because it goes to Default.aspx which is handled specially in MVC projects.

Additionally, you need to specify http://localhost/Test/Search/Index/1000. The controller and action pieces are not optional if you want to specify an ID.

like image 42
Sean Carpenter Avatar answered Oct 05 '22 22:10

Sean Carpenter