3

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
Slider345
  • 4,558
  • 7
  • 39
  • 47

3 Answers3

1

I would personally use the URL Rewriter. You can Redirect ASP.NET Legacy URLs to Extensionless with the IIS Rewrite Module.

So for example you could create an outbound rules that looked something like (not exactly, they definitely need testing):

  <outboundRules>
    <rule name="remove .mvc outbound 1">
      <match serverVariable="RESPONSE_LOCATION" 
             pattern="^/(.*).mvc$" />
      <action type="Rewrite" value="/{R:1}" />
    </rule>
    <rule name="remove .mvc outbound 2">
      <match filterByTags="A, Form, IFrame, Img, Input, Link, Script" 
             pattern="^/(.*).mvc$" />
      <action type="Rewrite" value="/{R:1}" />
    </rule>
  </outboundRules>

If someone decided to hardcode an MVC extension...

    <rule name="Add Slash Inbound">
      <match url="(.*)" />
      <conditions>
        <add input="{PATH_INFO}" pattern="^/(.*).mvc$" negate="true" />
      </conditions>
      <action type="Rewrite" url="{R:0}/" />
    </rule>
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
0

Looks like here is coded your solution with adding / at end of each requests Add a trailing slash at the end of each url?. If it is not enough We can try something else :)

Community
  • 1
  • 1
jan salawa
  • 1,198
  • 1
  • 8
  • 25
0

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?

Slider345
  • 4,558
  • 7
  • 39
  • 47