4

http://localhost:56472/test. throws the ugly yellow ASP.NET error message - The Resource Cannot be Found.

How do I catch this Request and

  1. Prevent the ugly yellow
  2. Strip the period from the Request and redirect?

I do have this in my Global.asax:

protected void Application_Error(object sender, EventArgs e) {
        // Do whatever you want to do with the error
}

But the error is not being caught. Normal 404s are caught here and I redirect to my custom error page successfully.

I'm deploying to Azure PaaS so not much granular control over IIS.

Update: My attempt at rewrite:

    <rule name="Strip Periods" stopProcessing="true">
      <match url="^(.*[^.])(\.+)$" />
      <conditions trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^site.com$" />
      </conditions>
      <action type="Redirect" url="{MapProtocol:{HTTPS}}://www.site.com/{R:1}" />
    </rule>
RobVious
  • 12,685
  • 25
  • 99
  • 181
  • The ugly yellow can be prevented by using a proper error page. There's a way to do it specific to MVC, and then a way that works with all of ASP.NET. As for the URL, is this a URL generated by your system? Or is the user typing it directly in the address bar? Or is some other system generating incorrect links to your site? – mason Jun 06 '16 at 17:25
  • @mason I'm aware of this and - as stated - I have custom error handling in place and it's working *except* for in this case. This is someone clicking a link that was incorrectly formatted to include a period at the end. – RobVious Jun 06 '16 at 17:30
  • But who created the link? Your site? Or some 3rd party? – mason Jun 06 '16 at 17:31
  • @mason Someone linked to my page and included the period accidentally. – RobVious Jun 06 '16 at 17:33
  • I suggest you use the [URL Rewrite Module](http://www.iis.net/downloads/microsoft/url-rewrite) to do a 301 redirect to the correct URL. This happens before the ASP.NET handler gets called, so it should work in this case (and you won't need to tinker with the error handling in your application). – NightOwl888 Jun 06 '16 at 18:08
  • @NightOwl888 - nice. I'm using that for http > https. Any idea what the markup would look like for stripping periods? That's one of the issues - redirecting. The bigger issue is preventing this ugly yellow from ever being displayed if another 'edge case' slips by. – RobVious Jun 06 '16 at 18:09
  • 3
    [How to do it using rewrite module](http://stackoverflow.com/a/2541677/163495) (from one of the related questions) – Richard Jun 06 '16 at 18:11
  • @Richard - updated question with my attempt. If you have any idea how that needs to change (if at all), feel free to post an answer and I'll accept. – RobVious Jun 06 '16 at 18:14

1 Answers1

2

You need two steps to do this. First, a rewrite rule to handle the redirection:

<rule name="RemoveTrailingDot" stopProcessing="true">
    <match url="(.*)\.+$" />
    <action type="Redirect" url="{R:1}" />
</rule>

Secondly, the following directive (found in this related answer)

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

The comments on the related answer suggest it doesn't always work, but it's working for me on IIS on Windows 10.

Community
  • 1
  • 1
Richard
  • 29,854
  • 11
  • 77
  • 120