Skip to content

Latest commit

 

History

History
24 lines (20 loc) · 1.05 KB

custom-http-status-codes.md

File metadata and controls

24 lines (20 loc) · 1.05 KB

Custom HTTP status codes in ASP.NET Core 3.x

The response status code can be set in requests handlers and it'll be honored by the composition pipeline. To set a custom response status code the following snippet can be used:

public class SampleHandlerWithCustomStatusCode : ICompositionRequestsHandler
{
    [HttpGet("/sample/{id}")]
    public Task Handle(HttpRequest request)
    {
        var response = request.HttpContext.Response;
        response.StatusCode = (int)HttpStatusCode.Forbidden;

        return Task.CompletedTask;
    }
}

snippet source | anchor

Note

Requests handlers are executed in parallel in a non-deterministic way, setting the response code in more than one handler can have unpredictable effects.