ASP.NET MVC in a virtual directory
Asked Answered
M

4

10

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?

Method answered 8/10, 2008 at 14:14 Comment(0)
K
4

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/

Kinghood answered 26/10, 2008 at 10:26 Comment(0)
U
2

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.

Unbraid answered 11/10, 2008 at 12:40 Comment(1)
Thanks for the reply. It is indeed a problem with IIS 5.x and I've actually got around it by using Visual Studio Development Server. It then works fine for both localhost and localhost/1000. If you want to edit out that last paragraph I'll mark your answer as accepted.Method
L
1

There are a number of considerations when using virtual directories in your application.

One is particular is that most browsers will not submit cookies that came from one virtual directory to another, even if the apps reside on the same server.

Luigi answered 8/10, 2008 at 14:18 Comment(2)
And the reason for the down vote is? Because the above is indeed a special consideration for the situation described.Luigi
I didn't give you a down vote, but your special consideration is common to all web apps, and the OP was asking specifically about ASP.NET MVC. In other words, your answer, while technically correct, isn't answering the question.Ba
A
1

Try set virtual path: right click on mvc project, properties, web tab, there enter appropriate location.

Appointor answered 8/10, 2008 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.