Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 841
Ann L. Avatar asked Nov 07 '11 21:11

Ann L.


People also ask

What is the use of virtual directory in asp net?

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.

What is a good place to register routes in an MVC application?

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.

Which method is used for adding routes to an MVC application?

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).


1 Answers

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
);
like image 130
David Brainer Avatar answered Nov 15 '22 19:11

David Brainer