a) add that last route to your route list. b) create a controller (in my example, i called it StaticContentController) with an Action method (in my example, i added a method called PageNotFound(..)) add logic this method to display the 404 page not found, View. If you still have the default route (I.E.
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.
I got my error handling to work by creating an ErrorController that returns the views in this article. I also had to add the "Catch All" to the route in global.asax.
I cannot see how it will get to any of these error pages if it is not in the Web.config..? My Web.config had to specify:
customErrors mode="On" defaultRedirect="~/Error/Unknown"
and then I also added:
error statusCode="404" redirect="~/Error/NotFound"
Source
NotFoundMVC - Provides a user-friendly 404 page whenever a controller, action or route is not found in your ASP.NET MVC3 application. A view called NotFound is rendered instead of the default ASP.NET error page.
You can add this plugin via nuget using: Install-Package NotFoundMvc
NotFoundMvc automatically installs itself during web application start-up. It handles all the different ways a 404 HttpException is usually thrown by ASP.NET MVC. This includes a missing controller, action and route.
Step by Step Installation Guide :
1 - Right click on your Project and Select Manage Nuget Packages...
2 - Search for NotFoundMvc
and install it.
3 - Once the installation has be completed, two files will be added to your project. As shown in the screenshots below.
4 - Open the newly added NotFound.cshtml present at Views/Shared and modify it at your will. Now run the application and type in an incorrect url, and you will be greeted with a User friendly 404 page.
No more, will users get errors message like Server Error in '/' Application. The resource cannot be found.
Hope this helps :)
P.S : Kudos to Andrew Davey for making such an awesome plugin.
Try this in web.config to replace IIS error pages. This is the best solution I guess, and it sends out the correct status code too.
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="404" path="Error404.html" responseMode="File" />
<error statusCode="500" path="Error.html" responseMode="File" />
</httpErrors>
</system.webServer>
More info from Tipila - Use Custom Error Pages ASP.NET MVC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With