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?
The virtual directory name becomes part of the application's URL, and users can request the URL from a browser to access content in the physical directory, such as a Web page or a list of additional directories and files.
Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder.
When an MVC application first starts, the Application_Start() method is called. This method, in turn, calls the RegisterRoutes() method. The RegisterRoutes() method creates the route table. The default route table contains a single route (named Default).
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
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With