ASP.NET MVC 3: Moved app into virtual directory. What do I have to change?

Folks,

I have been working on an MVC 3 app. I was using VS 2010's built-in web server. Today, for various reasons, I was asked to move it into a virtual directory and run it under IIS 7, still on my development PC.

Now that its URL is "localhost/MyVirtualDirectory" as opposed to "localhost:12345", what do I need to change to make routing work, and where?

I'm not using any raw HTML anchor tags or redirects, just @Html.ActionLink and so on. According to what I've read, if I've been doing things the MVC way, this change should have been transparent.

But right at the beginning, the post-authentication redirection fails. On successful authentication, it's supposed to return the result of

this.RedirectToAction("index", "Home")

You guessed it: instead of "/MyVirtualDirectory/Home" the redirection goes to "/Home". Which fails.

So something is missing that needs to be set up. What is it?

Thanks, all.


In IIS, choose your virtual directory and "Convert to Application." Also, if you are using the default route map in your Global.asax it should read something like this:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

Reasoning: If you put your MVC application in a sub-directory of another application then IIS will consider the root of that other application instead of the root of your MVC application. If that is the behavior that you want (unlikely) then you need to modify your Global.asax to take that into account:

routes.MapRoute(
    "Default", // Route name
    "MyVirtualDirectory/{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
链接地址: http://www.djcxy.com/p/9920.html

上一篇: Flex ++糟糕的字符错误等等。 新的flex

下一篇: ASP.NET MVC 3:将应用程序移入虚拟目录。 我需要改变什么?