Asp.net Mvc Custom 404 Error Page
Contents |
you're not alone. It's surprisingly difficult to do this correctly, not helped by the fact that some errors are handled by ASP.NET and
Asp.net Mvc 5 Custom Error Page
others by IIS. Ideally (and I expect such is the case asp.net mvc customerrors with some other frameworks/servers) we would just configure our custom error pages in one place and it would
Asp.net Mvc 4 Error Page
just work, no matter how/where the error was raised. Something like:
404 Page Not Found
I created a new ASP.NET MVC 5 application using the standard template in Visual Studio. If I run the site and try to navigate to a resource that does not exist e.g. /foo/bar, I'll get the standard ASP.NET 404 page with the following information: 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: /foo/bar Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.33440 Not exactly friendly, is it? In this case the error was raised by ASP.NET MVC because it could not find a matching controller and/or action that matched the specified URL. In order to set up a custom 404here for a quick overview of the site Help Center Detailed answers to any questions you might
Mvc 5 Error Page
have Meta Discuss the workings and policies of this site About mvc 5 redirect to error page Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads
Mvc Customerrors 404
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, http://benfoster.io/blog/aspnet-mvc-custom-error-pages just like you, helping each other. Join them; it only takes a minute: Sign up ASP.NET MVC 5 Custom Error Page up vote 19 down vote favorite 7 I am using a custom authorize attribute in a ASP.NET MVC 5 application like following: public class CustomAuthorizeAttribute : AuthorizeAttribute { protected override void HandleUnauthorizedRequest(AuthorizationContext context) { http://stackoverflow.com/questions/23565098/asp-net-mvc-5-custom-error-page if (context.HttpContext.Request.IsAuthenticated) { context.Result = new System.Web.Mvc.HttpStatusCodeResult((int)System.Net.HttpStatusCode.Forbidden); } else { base.HandleUnauthorizedRequest(context); } } } In system.web section of my web.config I mentioned error paths like:
/ Comments (1) While ASP.net MVC makes it easy and clean to create a professional http://www.mazsoft.com/blog/post/2014/07/16/Quick-and-easy-Custom-404-page-for-ASPnet-MVC-5 looking website, it seems to be a little harder to incorporate custome error pages (e.g. 404 page) which are part of general look and feel of your site. There are many good write-ups on how to do this (for example http://benfoster.io/blog/aspnet-mvc-custom-error-pages), but I was looking for an easy way to point to one of my views error page as the 404 page. What I ended up doing was to create a controller for my error views and use the httpErrors section of web.config to point to it: