2

I tried to implement a global error handler on my Asp.net core mvc web page. For that I created an error handler middleware like described on this blog post.

    public class ErrorHandlerMiddleware
{
    private readonly RequestDelegate _next;

    public ErrorHandlerMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        try
        {
            await _next(context);
        }
        catch (Exception error)
        {
            var response = context.Response;
            response.ContentType = "application/json";

            switch (error)
            {
                case KeyNotFoundException e:
                    // not found error
                    response.StatusCode = (int)HttpStatusCode.NotFound;
                    break;
                default:
                    // unhandled error
                    response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    break;
            }

            var result = JsonSerializer.Serialize(new { message = error?.Message });
            await response.WriteAsync(result);
            context.Request.Path = $"/error/{response.StatusCode}"; // <----does not work!
        }
    }
}

The middleware works as expected and catches the errors. As a result i get a white page with the error message. But i am not able to display a custom error page. I tried it with the following line of code. But this does not work.

context.Request.Path = $"/error/{response.StatusCode}";

Any ideas how I can achive my goal?

Thanks in advance

5
  • 1
    I think you want the response to redirect to $"/error/{response.StatusCode}" Commented Nov 30, 2021 at 15:53
  • Exactly, but how can I do that? context.Response has no path attribut. Commented Nov 30, 2021 at 15:56
  • 1
    Try context.Response.Redirect($"/error/{response.StatusCode}") Commented Nov 30, 2021 at 16:02
  • Thank you that worked. But I had to remove the line await response.WriteAsync(result); as well to get it running. Commented Dec 1, 2021 at 6:04
  • Yeah, there's no point writing to the response when you're redirecting the browser anyway. Commented Dec 1, 2021 at 8:46

1 Answer 1

1

It seems that you wish to redirect the browser to an error page.

To do this you'll need to replace:

context.Request.Path = $"/error/{response.StatusCode}";

With

context.Reponse.Redirect($"/error/{response.StatusCode}");

Also, since you're sending a redirect, the response content needs to be empty, so remove the response.WriteAsync bit too.

var result = JsonSerializer.Serialize(new { message = error?.Message });
await response.WriteAsync(result);
Sign up to request clarification or add additional context in comments.

2 Comments

not reponse. Response
@KOMODO Typo corrected :o)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.