1

I've seen many similar questions but they all focus on the first part of this task that I have already completed. Please let me know if I missed one that answers my question.

I have an IIS 7.5 server running a ASP .NET MVC5 site. As part of the routes I have added:

routes.MapRoute(
    name: "Default",
    url:"{*url}",
    defaults: new { controller = "Home", action = "NotFound" }
);

This route acts as my 404 action.

In addition in my web.config I have:

<modules runAllManagedModulesForAllRequests="true">

The resut of which is that I now have my 404 defined normally as an MVC action but it responds to all URLs including the following:

/error
/err.or
/e/r/r/o/r

However it came to my attention today that if I try this route it fails:

/error.

That is to say any route that ENDS with a period or dot gives me the the typical:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /error.

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34280

I have a similar setup in another project and it works for URLs that end in dots but I can't figure out what is different between the two projects. Any ideas what I might be doing wrong? or what's capturing this route? or what's excluding this route from being processed through ASP .Net?

Barak Gall
  • 1,422
  • 12
  • 24

1 Answers1

3

Most likely, the culprit is that you haven't enabled the setting:

<system.web>
  <httpRuntime relaxedUrlToFileSystemMapping="true" />
</system.web>

NOTE: According to this page, you don't need to enable runAllManagedModulesForAllRequests (which impacts performance) if your . is at the end of the query string.

Alternative

If that doesn't work, one way to effectively side-step the problem is to simply use the URL Rewrite Module to rewrite any URL whose path ends in a dot to /Home/NotFound. This eliminates the need to first respond with a 302 followed by a 404 (which is bad for SEO).

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Rewrite . at end of URL path to /Home/NotFound" stopProcessing="true">
          <match url="^.*\.(?:/?\?.*)?$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/Home/NotFound" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

This assumes that your action method that responds to /Home/NotFound properly sets the HTTP status code to 404.

References:

Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212