trailing slash issue in IIS 8.5

2020-04-14 09:56发布

问题:

We are moving a ASP.NET project from IIS6 (Win Server 2003) to IIS 8.5(Win Server 2012 R2). The project has some MVC components for which the following routing is used.

routes.MapRoute("simple", "{controller}.mvc/{action}/{id}");
routes.MapRoute(
                "Default", 
                "{controller}.mvc/{action}/{id}"                            
                new { controller = "Home", action = "Index", id = "" }
            );

Thus call to MyDemoController would be accessed by MyDemo.mvc

Now what happens, when I use the url as MyDemo.mvc/ it works, but when I use MyDemo.mvc without the slash it throws 404 error.

This happens only in the deployed server. In our local machines, which use IIS7.5, Win 7 it works without any issues.

Manually changing is not possible as there are lots of urls added to the sitemap file and our client does not approve the approach.

Is it something specific to the IIS version or any small tweak would solve the issue?

回答1:

Take a look at the following. I believe this is your problem. It has to do with the .mvc extension.

ASP.NET MVC - Routing - an action with file extension

Dots in URL causes 404 with ASP.NET mvc and IIS

The problem is that IIS will handle the .mvc file as a static file and will by default not route the hypothetical .mvc file through your MVC application. IIS handles the request and your MVC code never gets a change to route to this file.

In summary, here's what the configuration looks like to make .mvc files work:

<system.webServer>
  <handlers>
    <add name="MVCFileHandler"
      path="*.mvc"
      verb="GET" type="System.Web.Handlers.TransferRequestHandler"
      preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0"  />
  </handlers>
</system.webServer>

As for it working on IIS7.5, Win 7 without any issues and not IIS 8.5.

take a look at this answer

Routing a url with extension in MVC4 won't work, tries to serve up static file

There is also <modules runAllManagedModulesForAllRequests="true"> but it doesn't seem to work for MVC4/IIS8 (used to be ok in MVC3/IIS7 IIRC). More info here. There is also a performance impact with this one as every request will route through the managed pipeline.

Hope all this helps