Search code examples
asp.net-mvcasp.net-mvc-3.net-4.0iis-7.5

MVC routing for both .mvc and extensionless URLs


I have an odd question. I'm working on some MVC code that was setup to run extensionless, as in /Home/Index?id=10 However, it used to be setup for IIS 6, and was using the .mvc extension on all the controllers, as in /Home.mvc/Index?id=10.

I would like both routes to be able to work. In my global.asax.cs file, I created the following code:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute("Default2",
                    "{controller}.mvc/{action}/{id}",
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    routes.MapRoute("Default", 
                    "{controller}/{action}/{id}", 
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
    );
}

This almost worked! But unfortunately when my controllers return RedirectToAction, the URL which is generated fails.

I call RedirectToAction like this:

return RedirectToAction("Index", "Account");

and it returns this:

/Account.mvc

Now Index is assumed so I'm not worried that that's missing. However, there is no / on the end of that URL, and because of that, it is returning a 404 error. If I add the / it works fine. Is there some way I can help MVC to generate the URLs correctly, or is there a way I can modify the routes to make it recognize this URL?

Background: I'm in this situation because I began converting my application to use extensionless URLs after we upgraded our webserver to IIS 7.5. I didn't realize that there were alot of links into the applications which had been shared with the outside world--changing to extensionless broke all those links. Doh! Should've left well enough alone.


Solution

  • For anyone trying to figure this out: the problem may not be that RedirectToAction is generating a bad URL, but actually that the URL is not being interpreted properly by IIS.

    I discovered that other servers had no problem with the missing slash in the URLs described in the original question.

    This led me to look at the App Pool setting. One setting in particular seems to affect this: "Enabled 32-bit applications". Setting this to true on the Application Pool enabled my application to parse URLs which were missing the trailing '/'.

    Weird huh?