简体   繁体   中英

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. Also NotFound() and NoContent(). But I could not find something like Acceptable Response(406). So, my question is is it possible to return Acceptable Response(406) from API controller without using HttpResponseMessage.

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. Reason why I am trying to find another way is because in postman It shows wrong status code(Below Screenshot is given). 在此处输入图像描述

Here is a question that I think kind of what I am looking for Click Here . But I could not use CreateResponse in .NET 5.0 which I am currently working with.

Thank You.

You can return status codes using the following:

return StatusCode(StatusCodes.Status406NotAcceptable);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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