您可能认为这个问题可能会重复,但我找不到任何有用的答案。
这是我的 Controller 操作
[HttpPost]
[ResponseType(typeof(UserInputModel))]
public IHttpActionResult GetUsers(UserInputModel userModel)
{
if(adminBao.GetUsers(userModel)==null)
{
return NotFound();
}
return Ok(adminBao.GetUsers(userModel));
}
删除 [ResponseType(typeof(UserInputModel))] 后,输出有何不同
我知道这用于向 Web api 提示通用返回类型...但是它对响应有何影响?
有人可以帮我理解这一点吗
最佳答案
But what impact it make in the response?
它对实际响应没有影响。它主要用于文档目的。
其主要目的是让 ApiExplorer
知道在操作定义中使用抽象时操作返回的类型。
浏览器无法从 HttpResponseMessage
或 IHttpActionResult
或它们的派生之一确定这一点。
这样在生成文档时它可以显示正确的类型。
Use this to specify the entity type returned by an action when the declared return type is
HttpResponseMessage
orIHttpActionResult
. The ResponseType will be read by ApiExplorer when generating ApiDescription.
关于c# - Asp.Net Web API中ResponseTypeAttribute的主要用途是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45875265/