Asp.net Mvc3 404 Error Page
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about mvc 5 custom error page Stack Overflow the company Business Learn more about hiring developers or posting ads with
Mvc 404 Error Handling
us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is
Asp.net Mvc Custom Error Page
a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to create custom 404 Error pages in ASP.NET MVC 3? up vote 5
Set Custom Error Page In Web.config Mvc
down vote favorite 2 what is the best way to create custom error pages in ASP.NET MVC 3? The one I am particularly interested in is a 404 error, but also 403, and others. I am new to the MVC framework, traditionally I come from a PHP background, but am learning quickly. I did my research before posting this question and came across this link: Custom error pages on asp.net mvc redirect to error page MVC3 That solution seems simple although when I try to implement that on my machine, I get a problem with the following line: IController errorsController = new ErrorsController(); inside the Application_Error() function. It says "The type or namespace name 'ErrorsController' could not be found (are you missing a using directive or an assembly reference?". Thank you in advance for help you may provide. asp.net-mvc error-handling http-status-code-404 share|improve this question edited Feb 25 '13 at 10:41 tereško 42.4k1567123 asked Jun 28 '11 at 20:49 skcin7 3951619 possible duplicate of How can I properly handle 404 in ASP.NET MVC? –Jahangir Zinedine Oct 4 '13 at 7:31 add a comment| 2 Answers 2 active oldest votes up vote 15 down vote accepted You should configure
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 others by IIS. Ideally (and I expect asp.net custom error page such is the case with some other frameworks/servers) we would just configure our custom mvc 5 error handling error pages in one place and it would 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 404 error page add the following to web.config insidethe default HandleError Attribute in MVC. In this article, we will see how we can use the HandleError attribute to quickly put together http://www.devcurry.com/2012/06/aspnet-mvc-handling-exceptions-and-404.html an exception handling mechanism that helps hide the ‘dark' underbelly of your application but helps you debug with the same configuration. We will also look at a solution to handle 404 http://programmers.stackexchange.com/questions/45195/what-are-the-definitive-guidelines-for-custom-error-handling-in-asp-net-mvc-3 errors gracefully. Overall we try to achieve the following on IIS 7+ Goal 1 - Use MVCs HandleError action filter to take care of Unhandled/Unexpected errors Goal 2- See only error page user friendly messages at Runtime Goal 3 - See exception stack traces at debug time Goal 4- Manage 404 error with a proper error message Goal 5 - Keep a 404 Response status The HandleError Attribute The HandleError attribute helps mark controller classes for ‘Unhandled Error' exceptions. The HandleErrorAttribute() attribute filter will pipe the Unhandled exceptions to this attribute filter enabling you custom error page to take appropriate exception. The attribute filter is put in place by the default MVC template in the Global.asax (or for MVC4 in the App_Start\FilterConfig.cs) by adding the Action filter as follows: public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } By default, if you turn on CustomErrors, the behavior of the HandleErrorAttribute is to redirect you to the default Error page. Setting up the Custom Errors page Setup of the Custom Errors page is easy. We simply turn on the CustomErrors in the web.config. To Test the setting we throw an exception in the ‘About' action method of the Home Controller public ActionResult About() { ViewBag.Message = "Your app description page."; throw new ApplicationException("Testing default ErrorHandler attribute"); //return View(); } Now when we run the application and click on ‘About' we see the following This is the default output from Errors.cshtml in the Shared folder of a default MVC project. As we can see, it returns a correct 500 status message. At this stage, we have achieved the first and second goals we set out to achieve. However
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Programmers Questions Tags Users Badges Unanswered Ask Question _ Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top What are the definitive guidelines for custom Error Handling in ASP.NET MVC 3? up vote 43 down vote favorite 16 The process of doing custom error handling in ASP.NET MVC (3 in this case) seems to be incredibly neglected. I've read through the various questions and answers here, on the web, help pages for various tools (like Elmah), but I feel like I've gone in a complete circle and still don't have the best solution. With your help, perhaps we can set a new standard approach for error handling. I'd like to keep things simple and not over-engineer this. Here are my goals: For Server errors/exceptions: Display debugging information in dev Display friendly error page in production Log errors and email them to administrator in production Return 500 HTTP Status Code For 404 Not Found errors: Display friendly error page Log errors and email them to administrator in production Return 404 HTTP Status Code Is there a way to meet these goals with ASP.NET MVC? asp.net asp.net-mvc error-handling asp.net-mvc-3 share|improve this question asked Jan 21 '11 at 20:07 RyanW 5491410 migrated from stackoverflow.com Feb 7 '11 at 22:08 This question came from our site for professional and enthusiast programmers. 2 I'd like this question to be migrated BACK to SO so it can get more answers. I'm seeking coded answers. –Shawn Mclean May 13 '11 at 17:21 @Shawn That's unlikely to happen. The question is more on topic here than it is on SO and it has an accepted answer. Questions also generally don't get re-migrated for technical reasons. If you need help with coding a specific error-handling approach, please open a new question on StackOverflow. Otherwise, "coded answers" may be too broad a criteria to be useful or answerable. Last but not least, the best way to attract moderator attention to a question is to flag it. Your comment here likely would've gone unnoticed if it didn't trip an auto-flag by pushing the comment count over 20. –Adam Lear♦ May 13 '11 at 17:4