Mvc2 Default Error Page
Contents |
Dustin ASP .NET MVC Recently, I had to implement a dynamic custom 404 error page in an ASP.NET MVC2 website. My first instinct was to turn CustomErrors mode on and setup a 404 Error, but this failed to return my custom error page in a custom error page mvc shared hosting environment and instead displayed the default IIS 404 page. I'll walk you through fixing this
Mvc 5 Custom Error Page
in just a minute, but for now I want to show you how to create the custom 404 page. Creating the View The first step
Mvc Redirect To Error Page
in setting up a dynamiccustom 404 is to create your view. In my case I just added a new View to the project called 404 which created 404.aspx in my root directory (I did not add it to the Views folder, but
Set Custom Error Page In Web.config Mvc
you can if you'd like). I kept the 404 page outside of the default Views folder as it wasn't part of my essential application. My 404 View used my public site Master Page which has a dynamic menu. Creating the Controller The next step in setting up the custom 404 page was creating a controller to handle it. My site uses many custom routes, including one for {category}/{urlSlug} so I loaded the View based on the file path and executed this controller when needed for mvc 5 error handling the other pages. In my case I will never have any static pages so my {category}/{urlSlug} route will catch everything that is not specifically mapped to other sections (such as my Admin area). For my 404, I just used my HomeController and added a PageNotFound method. The PageNotFound method is extremely simple. It is responsible only for setting the StatusCode of the response to 404 and returning the 404.aspx page that I created: [HttpGet] public ViewResult PageNotFound() { Response.StatusCode = 404; return View("~/404.aspx"); } Mapping the 404 Route In my case I can have any number of dynamic routes based on a supplied category and urlSlug (remember my {category}/{urlSlug} route?). Because of this, I need to specifically create a route for my 404 page. This route needs to be above all other routes to ensure it gets processed: routes.MapRoute(null, "404", new { controller = "Home", action = "PageNotFound" }); Now the 404 page can be accessed via http://exampledomain.com/404. This is perfect as it makes it easy for me to access from my dynamic content method. Triggering the 404 Page The 404 page is triggered by simply throwing an HttpException from code. In my case, an attempt is made to load a content page from the database. If no content page exists, an HttpException is thrown. For the sake of this article I'm going to sidetrack a bit and teach you how to check whether a view exists and throw an HttpException if it doesn't. In my example, under Views I've creat
here for a quick overview of the site Help Center Detailed answers to any questions asp.net custom error page you might have Meta Discuss the workings and policies of mvc 5 redirect to error page this site About Us Learn more about Stack Overflow the company Business Learn more about hiring asp.net mvc 404 page developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is http://www.dustinhorne.com/post/2011/06/14/Dynamic-Custom-Error-Pages-in-ASPNET-MVC-2 a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up MVC Routing and Custom Error Page Not Found Problem up vote 0 down vote favorite I am using my own controller factory in an MVC 2 app, and this standard code which http://stackoverflow.com/questions/6590524/mvc-routing-and-custom-error-page-not-found-problem seems to be going around: protected override IController GetControllerInstance(RequestContext reqContext, Type controllerType) { ... // Error handling if (controllerType == null) { throw new HttpException( 404, String.Format( "The controller for path '{0}' could not be found" + " or it does not implement IController.", reqContext.HttpContext.Request.Path ) ); } ... } The problem I was having is one others have asked about, where certain resources where being caught by this error handling logic and my ELMAH has been getting populated with hundreds of pointless errors. More annoyingly, it makes debugging painful to have to keep F5-ing the errors. These resources are: favicon.ico, Error.aspx (my custom error page), and some images in a particular folder. So I followed the advice of this post, and attempted to solve the issues through ignoring routes in the global.asax: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("elmah.axd"); routes.IgnoreRoute("/Content/images/{*pathinfo}"); // This has succesfully rid me of a bunch of errors routes.IgnoreRoute("{*favicon}", new {
30, 2013 by trailmax | 3 Replies It is vital for your application security not to show any internals when error happen. And you should be able to replace all internal error messages to http://tech.trailmax.info/2013/08/error-handling-in-mvc-and-nice-error-pages/ nice user-friendly pages. It is a just nice for users - they are not https://devstuffs.wordpress.com/2010/12/12/how-to-use-customerrors-in-asp-net-mvc-2/ getting splashes of oil, when engine is exploded, also another measure to improve site security. There are lot of articles about error handling in ASP.Net MVC, but most of them do not cover the whole range. There is a very good resource on this, and I do recommend reading and understanding that first. With error handling there error page are a lot of edge cases, and for every single one of them you need to provide a solution, otherwise your error messages will talk too loud about your implementation and that can lead to security vulnerability. Upd 18/03/2016 There are a ton of similar articles on this topic. Here are some nice ones: Ben Foster - probably this one is the most comprehensive and worth reading first. Mahesh Sabnis Milevis Here custom error page is the list of edge cases I came up with: Exception thrown in controller Controller or controller action is not found Page not found, but outside of the MVC pipeline Exception in IIS pipeline Cases when IIS can't handle the request all together. Exception thrown in controller. When exceptions are thrown in your code, most of the time they will be thrown in MVC pipeline and handled by MVC error handling mechanisms. First of all you need enable CustomErrors in web.config:
and after that I will show you how to create the pages.