Kieron Kieron - 4 years ago 231
HTTP Question

Set the reason on a HttpResponse in ASP.NET Core from middlware

How can I set the reason phrase on a HTTP response in ASP.NET Core from some middleware?

In previous versions (full framework), I would do the following:

context.Response.StatusCode = 401;
context.Response.ReasonPhrase = "Missing or invalid token.";

In ASP.NET Core, the only property available is the

Here is an example of a Fiddler capture with a custom reason set on the response.

Example of a reason sent from ASP.NET WebAPI (full framework)

Answer Source

Try this:

context.Response.StatusCode = 401;
context.Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "Missing or invalid token.";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download