Automatic redirect on http status code

Using UseStatusCodePagesWithRedirects to automatically redirect to an error endpoint

Home DailyDrop

Daily Knowledge Drop

The UseStatusCodePagesWithRedirects method can be used to redirect the client to an error page in the case of a non-successful request (400-599 range)

This method returns a 301-Found to the client, and then redirects to the redirect endpoint which will return a status code of 200-Success.

This is often used when the app:

  • Should redirect the client to a different endpoint, perhaps to have a different application process the error. In the browser, the redirect endpoint will be reflected
  • Doesn't need to preserve and return the original status code with the initial redirect response back to the client

Middleware setup

First step is to enable the automatic redirect functionality - this step is very straight-froward, and entails adding a single middleware components:

var app = builder.Build();

// other middleware setup goes here
app.UseStatusCodePagesWithRedirects("/error?status={0}");
// other middleware setup goes here

Here, the middleware is instructed to redirect any responses (which are not successful) to the /error endpoint. The endpoint can include a {0} placeholder which will contain the http status code.


Error endpoint

Next, up the /error endpoint is defined.

Here the Map method is used - this takes an IApplicationBuilder as a parameter:

app.Map("/error", errorApp =>
{
    errorApp.Run(async context =>
    {
        await context.Response.WriteAsync($"This is a redirected " +
            $"error message status {context.Request.Query["status"]}");
    });
});

For all error responses, the message will be output along with the http status code.


Execution

A number of other endpoints were also defined to simulate different status code responses:

app.MapGet("/500response", context =>
{
    context.Response.StatusCode = 500;
    return Task.CompletedTask;
});

app.MapGet("/401response", context =>
{
    context.Response.StatusCode = 401;
    return Task.CompletedTask;
});

app.MapGet("/200response", context =>
{
    context.Response.StatusCode = 200;
    return Task.CompletedTask;
});

Browsing to the /500response from a browser, for example, will return the following:

This is a redirected error message status 500

And looking at the Network tab of the browse, one can see that the initial status code returned is 302, followed by the 200 on redirect

HTTP Status code

Now, on error (500 status code), the caller is routed to a generic error page, with a 200 response, and the actual error in the content of the page.


Notes

For a lot of situations this might not be especially useful, as the return message and status code is not especially useful to the calling application to act on.

For a customer facing application this could be useful (providing the actual error is logged and recorded somewhere) as the customer doesn't care about the details of the eror, but for a backend api-to-api call, the string response is not especially useful.


References

Status Pages
UseStatusCodePages

Daily Drop 178: 10-10-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net httpstatus statuscode