Set the reason on a HttpResponse in ASP.NET Core f

2019-06-28 02:17发布

问题:

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 StatusCode.

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

回答1:

Try this:

context.Response.StatusCode = 401;
context.Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "Missing or invalid token.";