简体   繁体   English

不可接受的响应 (406) 消息 Asp.NET 代码 Web API (C#) - Z303CB0EF9EDB9082D61BB0572AZ

[英]Not Acceptable Response(406) Message Asp.NET Code Web API (C#) - .NET 5.0

Normally I use Ok() to return the 200OK message form a API controller.通常我使用 Ok() 从 API controller 返回 200OK 消息。 Also NotFound() and NoContent().还有 NotFound() 和 NoContent()。 But I could not find something like Acceptable Response(406).但我找不到像 Acceptable Response(406) 这样的东西。 So, my question is is it possible to return Acceptable Response(406) from API controller without using HttpResponseMessage.所以,我的问题是是否可以在不使用 HttpResponseMessage 的情况下从 API controller 返回可接受的响应(406)。

I know I can use something like HttpResponseMessage(System.Net.HttpStatusCode.NotAcceptable) but I want to know if there is a better approach which covers all the status codes and a custom error message can be returned.我知道我可以使用 HttpResponseMessage(System.Net.HttpStatusCode.NotAcceptable) 之类的东西,但我想知道是否有更好的方法可以覆盖所有状态代码并且可以返回自定义错误消息。 Reason why I am trying to find another way is because in postman It shows wrong status code(Below Screenshot is given).我试图寻找另一种方法的原因是因为在 postman 它显示错误的状态代码(给出了以下屏幕截图)。 在此处输入图像描述

Here is a question that I think kind of what I am looking for Click Here .这是一个我认为我正在寻找的问题Click Here But I could not use CreateResponse in .NET 5.0 which I am currently working with.但我无法在我目前正在使用的 .NET 5.0 中使用 CreateResponse。

Thank You.谢谢你。

You can return status codes using the following:您可以使用以下方式返回状态代码:

return StatusCode(StatusCodes.Status406NotAcceptable);

return new ObjectResult("Some message or object") { StatusCode = StatusCodes.Status406NotAcceptable };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM