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

2019-06-28 02:31发布

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.

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

1条回答
时光不老,我们不散
2楼-- · 2019-06-28 02:44

Try this:

context.Response.StatusCode = 401;
context.Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "Missing or invalid token.";
查看更多
登录 后发表回答