从中间件设置 ASP.NET Core 中 HttpResponse 的原因 [英] Set the reason on a HttpResponse in ASP.NET Core from middlware

查看:40
本文介绍了从中间件设置 ASP.NET Core 中 HttpResponse 的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在来自某些中间件的 ASP.NET Core 中的 HTTP 响应上设置原因短语?

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.";

在 ASP.NET Core 中,唯一可用的属性是 StatusCode.

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

这是一个 Fiddler 捕获示例,在响应中设置了自定义原因.

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

推荐答案

试试这个:

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

这篇关于从中间件设置 ASP.NET Core 中 HttpResponse 的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆