ASP.NET MVC in a virtual directory

我的梦境 提交于 2019-12-04 10:36:48

问题


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?


回答1:


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/




回答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.




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/182993/asp-net-mvc-in-a-virtual-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!