Asp.net Mvc 401 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 Stack Overflow the company aspnet mvc nuget Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs aspnet mvc source Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, aspnet mvc tutorial just like you, helping each other. Join them; it only takes a minute: Sign up ASP.NET MVC4 error handling for 404, 401 and other exceptions up vote 3 down vote favorite I'm struggling to understand how to
Aspnet Mvc 5
correctly handle errors in ASP.NET MVC4. As an example, I've created a new MVC4 project using the "Internet Application" template and updated my home controller to test out some error cases: public class HomeController : Controller { public ActionResult Index() { ViewBag.Message = "Hello"; return View(); } public ActionResult About() { throw new HttpException(401, "Not Authorized"); } public ActionResult Contact() { throw new Exception("Oh no, some error occurred..."); } } I have enabled customErrors aspnet mvc 4 in my web.config file:
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings aspnet mvc cms and policies of this site About Us Learn more about Stack Overflow
Aspnet Mvc 6
the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags
Aspnet Mvc 3
Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only http://stackoverflow.com/questions/13607543/asp-net-mvc4-error-handling-for-404-401-and-other-exceptions takes a minute: Sign up ASP.NET MVC user friendly 401 error up vote 15 down vote favorite 8 I have implemented errors handling in ASP.NET MVC site in a way like suggests this post. With 404 errors all works fine. But how correctly show user friendly screen for a 401 error? They usually do not throw Exception that can be http://stackoverflow.com/questions/1679881/asp-net-mvc-user-friendly-401-error handled inside Application_Error() but rather action returns HttpUnauthorizedResult. One possible way is to add following code to the end of Application_EndRequest() method if (Context.Response.StatusCode == 401) { throw new HttpException(401, "You are not authorised"); // or UserFriendlyErrorRedirect(new HttpException(401, "You are not authorised")), witout exception } But inside Application_EndRequest() Context.Session == null, errorController.Execute() fails because it cannot use default TempDataProvider. // Call target Controller and pass the routeData. IController errorController = new ErrorController(); errorController.Execute(new RequestContext( new HttpContextWrapper(Context), routeData)); // Additional information: The SessionStateTempDataProvider requires SessionState to be enabled. So, can you suggest some best practices how to 'user friendly handle' 401 in ASP.NET MVC application? Thanks. asp.net-mvc handleerror share|improve this question edited Mar 18 '11 at 3:53 Pure.Krome 37.3k69262457 asked Nov 5 '09 at 11:17 Roman 1,86872651 stackoverflow.com/questions/2580596/… –Cherian Apr 24 '10 at 13:21 add a comment| 4 Answers 4 active oldest votes up vote 14 down vote accepted Look at the HandleErrorAttribute. Subclass from it or add your own implementation which will handle all the status codes you're interested in. You can make it to return a
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 such is the case with some other frameworks/servers) http://benfoster.io/blog/aspnet-mvc-custom-error-pages we would just configure our custom 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 asp.net mvc 401 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 insidePortfolio FAQs Blog Contact Get a Quote Home About Me Services Website Design Responsive Web Design Website Maintenance Web Hosting WordPress Services Professional Writing Services Search Engine Optimization Charitable Organizations Pricing Pricing Portfolio FAQs Blog Contact Get a Quote Home About Me Services Website Design Responsive Web Design Website Maintenance Web Hosting WordPress Services Professional Writing Services Search Engine Optimization Charitable Organizations Pricing Pricing Portfolio FAQs Blog Contact Get a Quote Home About Me Services Website Design Responsive Web Design Website Maintenance Web Hosting WordPress Services Professional Writing Services Search Engine Optimization Charitable Organizations Pricing Pricing Portfolio FAQs Blog Contact Get a Quote Call (608)-616-9872 FacebookTwitter Custom 401 error pages in MVC alongside Windows Authentication Custom 401 error pages in MVC alongside Windows Authentication ASP.NET IIS MVC // Brent // February 3, 2015 // no comments We use windows authentication for a lot of our web apps, but also want to serve up a custom 401 error page when users are not authorized to log in. This is tricky because this specific 401 error is handled directly by IIS, rather than the application itself, because it's a windows directory permission. I spent quite a bit of time searching for an elegant solution to this, but really came up with nothing. This is one way I've found to circumvent the issue, and serve up a custom 401, while still preserving your MVC application level error handling: 1) You will need an entry for your 401 error page in the httpErrors section of web.config: