Error handling in ASP.NET 5

By Piotr on (tags: ASP.NET5, error, mvc6, web, categories: web)

There is a lot of changes since MVC5 regarding error handling. I will show you how to catch and handle some basic errors. Here is a nice article by Krzysztof about error handling in MVC5.

First of all let me explain what “middleware” is…. or even better – here is the definition of middleware provided by OWIN from here:

Middleware is our component which we can plug in to request pipeline. We will use it to handle errors.

500 – internal server error

This is the simple one, we can just use builtin middleware “UseErrorHandler”. Modify Startup.cs as below:

As you can there is nice way to handle basic exception and invoke our controller. In this case Home controller and Error action.

Our Error action will look like:

Look at line “var error = Context.GetFeature<IErrorHandlerFeature>();”. The IErrorHandlerFeature interface has only one property: Exception – isn’t that just nice? Now we have exception context in our controller.

400 – bad request

This type of error requires from us to use different middleware – which is also builtin: “UseStatusCodePagesWithReExecute”.

Startup.cs:

What is happening here? Middleware will re-execute all request, which response has some HTTP error code and has empty body - to our controller. The “{0}” is placeholder for HTTP status code, so in this example, every response which has 400 code will be automatically redirected to http://localhost/Errors/Error400.

This is very basic scenario. If we want to do some more complex stuff we will need to write a custom middleware

404 – not found

This is specific error. First of all we need to catch it before IIS will. All not-handled requests will be handled by IIS by default (which is normal). That means that we need to handle all requests regardless if the route exists or not. Below I wrote simple RequestDelegate and I attached it as a last in pipeline. If our request won’t be handled by any of our routes then we know that the resource doesn’t exist:

That simple code will do two things – prevent request to be handled by IIS and will set status of response to 404. Thanks to our previous way of handle errors (app.UseStatusCodePagesWithReExecute("/Errors/Error{0}");) the request will be redirect to http://localhost/Errors/Error404.

Conclusion

Now you know how to handle basic error types. If you need to handle some advanced scenario then you will need to write you own middleware, which is rather simple task. That mechanism gives us a lot of controll and I belive is very simple to uderstand.