c# - Asp.Net Web API中ResponseTypeAttribute的主要用途是什么

标签 c# asp.net-web-api

您可能认为这个问题可能会重复,但我找不到任何有用的答案。

这是我的 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 知道在操作定义中使用抽象时操作返回的类型。

浏览器无法从 HttpResponseMessageIHttpActionResult 或它们的派生之一确定这一点。

这样在生成文档时它可以显示正确的类型。

ResponseTypeAttribute Class

Use this to specify the entity type returned by an action when the declared return type is HttpResponseMessage or IHttpActionResult. The ResponseType will be read by ApiExplorer when generating ApiDescription.

关于c# - Asp.Net Web API中ResponseTypeAttribute的主要用途是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45875265/

相关文章:

javascript - 为什么我的行设置为可见后不可见?

c# - 有选择地为 RichTextBox 中的文本着色

c# - 如何将 IList<MyObject> 作为参数传递给 Web API 中的 POST 方法?

c# - 在 Web Api Controller 中使用重定向(发现 HTTP 302)

c# - .NET Core 2.1 Web API 是否支持基于约定的路由?

c# - CORS 预检选项请求从 Windows 身份验证的 web api 返回 401(未经授权)

C# 命名空间别名 - 有什么意义?

c# - httphandler 无法正常工作

c# - 具有 ExpectedException 的 XUnit 和 MSTest 返回不同的结果

c# - 使用 C#.net 在 winform 中调用和使用 Web API