I am trying to make a custom HTTP 404 error page when someone types in a URL that doesn't invoke a valid action or controller in ASP.NET MVC, instead of it displaying the generic "Resource Not Found" ASP.NET error.
I don't want to use the web.config to handle this.
Is there any kind of routing magic I can do to catch any invalid URLs?
Update: I tried the answer given, however I still get the ugly "Resource Not Found" message.
Another update: Ok, apparently something changed in RC1. I've even tried specifically trapping 404 on an HttpException
and it still just gives me the "Resource Not Found" page.
I've even used MvcContrib's resource's feature and nothing - same problem. Any ideas?
I've tried to enable custom errors on production server for 3 hours, seems I found final solution how to do this in ASP.NET MVC without any routes.
To enable custom errors in ASP.NET MVC application we need (IIS 7+):
Configure custom pages in web config under system.web
section:
<customErrors mode="RemoteOnly" defaultRedirect="~/error">
<error statusCode="404" redirect="~/error/Error404" />
<error statusCode="500" redirect="~/error" />
</customErrors>
RemoteOnly
means that on local network you will see real errors (very useful during development). We can also rewrite error page for any error code.
Set magic Response parameter and response status code (in error handling module or in error handle attribute)
HttpContext.Current.Response.StatusCode = 500;
HttpContext.Current.Response.TrySkipIisCustomErrors = true;
Set another magic setting in web config under system.webServer
section:
<httpErrors errorMode="Detailed" />
This was final thing that I've found and after this I can see custom errors on production server.