4

In an ASP.NET MVC 4 application, we have set up a catch-all route as follows:

routes.MapRoute( 
     name: "UnKnown",  
     url: "{*url}",  
     defaults: new { controller = "CatchAll", action = "UnknownUrl" });

The UnknownUrl method in the CatchAllController correctly loads its view in our development environment.

However, the production IIS 7.5 shows its standard 404 page if a non-existing remote request arrives. A local request, sent using RDP on the server itself, works fine.

The web.config is set tp

<customErrors mode="Off"/>

What other difference is there between a local call and a remote call? How can we make the MVC HttpHandler catch those requests?

A hint might be that we were also unable to make the IIS show any detailed status 500 error messages when called remotely.

Olaf
  • 10,049
  • 8
  • 38
  • 54

2 Answers2

3

I have had problems with IIS showing default errors instead of .NET errors, which I've fixed with the following in system.webServer in the web.config:

<httpErrors existingResponse="PassThrough"/>

I think this would happen if in your UnknownUrl action you are setting Response.StatusCode = 404;. By default IIS sees you are returning an error code so shows a default error message, which you can override with that config setting.

I'm not sure this would be different on local v remote but could be worth a try.

Ian Routledge
  • 4,012
  • 1
  • 23
  • 28
  • Very cool. That did it for us and also solved the error issue! – Olaf Apr 24 '13 at 13:59
  • BTW, in order to allow settings in the httpErrors section, in IIS 7.0 one may have to execute cd C:\Windows\System32\inetsrv and appcmd unlock config /section:httpErrors as described in http://stackoverflow.com/questions/2345532/turn-iis7-http-error-handling-off. – Olaf Apr 24 '13 at 14:01
1

Can you try setting the host header - I believe this is what causes the difference between local and production:

new { controller = "CatchAll", action = "UnknownUrl", host = HttpContext.Current.Request.Url.Host}

Register this route from Application_BeginRequest of Global.asax. Also, ensure that this is done only once - perhaps by a check similar to:

if (routes["UnKnown"] == null)
        {
            routes.MapRoute( 
                name: "UnKnown",  
                url: "{*url}",
                defaults: new { controller = "CatchAll", action = "UnknownUrl", host = HttpContext.Current.Request.Url.Host}
            );
        }
aquaraga
  • 4,138
  • 23
  • 29
  • Thanks. We've added this, but it throws an error, saying "Request is not available in this context" (in German: "Anforderung steht in diesem Kontext nicht zur Verfügung"). Any ideas? – Olaf Apr 24 '13 at 08:14
  • Oh okay, that's because HttpContext wouldn't be available during Application_Start. You could probably map this 'catch-all' route within "void Application_BeginRequest()" in your Global.asax. – aquaraga Apr 24 '13 at 08:30
  • Too bad - it's still not working. Placing it into Application_BeginRequest didn't work, so we set the host manually by reading the web.config. But still the old 404. – Olaf Apr 24 '13 at 11:11