asp.net-core

Error Handling

Redirect to custom error page

ASP.NET Core provides the status code pages middleware, that supports several different extension methods, but we are interesting in UseStatusCodePages and UseStatusCodePagesWithRedirects:

  • UseStatusCodePages adds a StatusCodePages middleware with the given options that checks for responses with status codes between 400 and 599 that do not have a body. Example of use for redirect:

    app.UseStatusCodePages(async context => {
      //context.HttpContext.Response.StatusCode contains the status code
    
      // your redirect logic
    
    });
  • UseStatusCodePagesWithRedirects adds a StatusCodePages middleware to the pipeline. Specifies that responses should be handled by redirecting with the given location URL template. This may include a ‘{0}’ placeholder for the status code. URLs starting with ’~’ will have PathBase prepended, where any other URL will be used as is.

For example the following will redirect to ~/errors/<error_code> (for example ~/errors/403 for 403 error):

  app.UseStatusCodePagesWithRedirects("~/errors/{0}");

Global Exception Handling in ASP.NET Core

UseExceptionHandler can be used to handle exceptions globally. You can get all the details of exception object like Stack Trace, Inner exception and others. And then you can show them on screen. You can easily implement like this.

app.UseExceptionHandler(
 options => {
    options.Run(
    async context =>
    {
      context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
      context.Response.ContentType = "text/html";
      var ex = context.Features.Get<IExceptionHandlerFeature>();
      if (ex != null)
      {
        var err = $"<h1>Error: {ex.Error.Message}</h1>{ex.Error.StackTrace }";
        await context.Response.WriteAsync(err).ConfigureAwait(false);
      }
    });
 }
);

You need to put this inside configure() of startup.cs file.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow