Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC4 error handling for 404, 401 and other exceptions

I'm struggling to understand how to 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 in my web.config file:

<customErrors mode="On"></customErrors>

When I run the app and click "Contact", I see the ~/Views/Shared/Error.cshtml view as expected, since I have the HandleErrorAttribute registered as a global filter.

However, when I click "About", I get the standard ASP.NET yellow error page that says "Runtime Error". Why are these two exceptions being handled differently and how can I get instances of HttpException to get caught using the HandleError attribute?


CustomErrors config

Ideally, I'd like custom error pages for the following:

  • A custom 404 (not found) page that's nice and user friendly
  • A custom 401 (not authorised) page that informs the user that they do not have access (e.g. thrown after checking permissions for a particular item in the model)
  • A generic error page that is used in all other cases (in place of the standard yellow ASP.NET page).

I've created a new "Error" controller with views for each of the scenarios above. I have then updated customErrors in web.config like so:

<customErrors mode="On" defaultRedirect="~/Error/Trouble">
    <error statusCode="404" redirect="~/Error/NotFound"></error>
    <error statusCode="401" redirect="~/Error/NotAuthorized"></error>
</customErrors>

The 404 page works fine, but I don't get the 401 page at all. Instead, I get the ~/Error/Trouble view (the one specified as the defaultRedirect) when I try to access the About action on the Home controller.

Why is my custom 401 redirect page not working?

like image 665
Matt Wilson Avatar asked Nov 28 '12 14:11

Matt Wilson


People also ask

How to handle 404 errors in MVC with MVC?

404 Errors are a little trickier to handle in ASP.NET MVC. We will walk through the following steps to nail all kinds of 404 errors in our application Step 1: To start off with, we will update our Web.config to route 404 errors to a different view called ‘FailWhale’

Why is the 404 error not available in the errorcontroller?

The 404 error is not available in the above ErrorController because ASP.NET has already handled the error, bubbled it up to the routing framework that is using the web.config setting to route the request to the ‘redirected page’. Essentially it is a 302 redirection.

How to handle 404 error in Salesforce?

Add ErrorControllers or static page to with 404 error information. Modify your web.config (in case of controller). This will handle both missed routes and missed actions. Show activity on this post.

How do I handle unhandled exceptions in MVC?

These filters handle any unhandled exception that occurs during the execution of a controller action or another filter. For more information, see Filters in ASP.NET Core. Tip Exception filters are useful for trapping exceptions that occur within MVC actions, but they're not as flexible as the Exception Handling Middleware.


2 Answers

ASP.NET uses 401's internally to redirect users to the login page. Wherever you were planning to throw a 401 unauthorized, instead throw a 403 forbidden.

like image 188
danludwig Avatar answered Nov 10 '22 17:11

danludwig


If you really need to return a 401 and not a 403, then you can use:

HttpContext.Current.Response.SuppressFormsAuthenticationRedirect = true
like image 34
Monstieur Avatar answered Nov 10 '22 17:11

Monstieur