简体   繁体   English

如何在ASP.NET Core中返回自定义HTTP状态/消息而不返回对象,IActionResult等?

[英]How to return a custom HTTP status/message in ASP.NET Core without returning object, IActionResult, etc?

I have an ASP.NET Core Web API site, with Swagger generation and UI enabled. 我有一个ASP.NET Core Web API站点,启用了Swagger生成和UI。 In order for Swagger to work (at least work automatically), the return value from the controller method must be typed. 为了使Swagger工作(至少自动工作),必须输入控制器方法的返回值。 For example, 例如,

public async Task<Employee> LoadEmployee(string id)

However, I need to return custom HTTP status codes and content from this action. 但是,我需要从此操作返回自定义HTTP状态代码和内容。 All the examples I've seen use the StatusCode method, or return some other object. 我见过的所有示例都使用StatusCode方法,或者返回其他一些对象。 The problem with this is then Swagger doesn't know what the return type of the action is, and so can't generate the API spec. 这个问题就是Swagger不知道动作的返回类型是什么,因此无法生成API规范。

Is there some way (Exception, methods on controller, etc) to return the custom code/content, while keeping the signature? 是否有某种方式(异常,控制器上的方法等)返回自定义代码/内容,同时保持签名? I've seen solutions using custom middleware, but it seems like a common enough scenario that there should be something built it. 我见过使用自定义中间件的解决方案,但似乎应该有一些内置的东西。

You can just use StatusCodeResult StatusCode(...) to return status code and a message/object. 您可以使用StatusCodeResult StatusCode(...)返回状态代码和消息/对象。

public async Task<ObjectResult> LoadEmployee(string id)
{
    var employee = await repository.GetById(id);
    if(employee == null) {
        return NotFound();
    }

    return StatusCode((int)HttpStatusCode.Ok, employee);
}

references: 引用:

ASP.NET Core APIs in the fast lane with Swagger and Autorest 使用Swagger和Autorest的快速通道中的ASP.NET Core API

Adding swagger in ASP.NET Core Web API 在ASP.NET Core Web API中添加swagger

ASP.NET Core 1.0 MVC API documentation using Swashbuckle Swagger 使用Swashbuckle Swagger的ASP.NET Core 1.0 MVC API文档

For output definition, just add the [Produces] and [SwaggerResponse] attributes describing the Type returned, like this: 对于输出定义,只需添加描述返回类型的[Produces][SwaggerResponse]属性,如下所示:

[HttpGet]
[Produces(typeof(Employee))]
[SwaggerResponse(System.Net.HttpStatusCode.OK, Type = typeof(Employee))]
public async Task<IActionResult> LoadEmployee(string id) {
    var employee = await repository.GetById(id);
    if(employee == null) {
        return NotFound();
    }
    return Ok(employee);
}

ProducesResponseType attribute is supported by Swagger and is a MVC Web API Core attribute, not Swagger. Swagger支持ProducesResponseType属性,它是MVC Web API Core属性,而不是Swagger。 Does the same as SwaggerResponse . SwaggerResponse相同。

暂无
暂无

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

相关问题 如何在 ASP.NET Core 中返回 403 Forbidden 响应作为 IActionResult - How to return 403 Forbidden response as IActionResult in ASP.NET Core ASP.NET 核心任务<iactionresult>没有异步就无法返回 NotFound() 和 OK()</iactionresult> - ASP.NET Core Task<IActionResult> cannot return NotFound() and OK() without async 从ASP.NET Core中的WebSocket请求返回什么IActionResult? - What IActionResult to return from a WebSocket request in ASP.NET Core? 如何将此IHttpActionResult转换为ASP.NET Core IActionResult - How to translate this IHttpActionResult to ASP.NET Core IActionResult 如何测试我的 ASP.NET Core 2.2 Web API GET IActionResult 返回 Ok(object)? - How to test my ASP.NET Core 2.2 Web API GET IActionResult that returns Ok(object)? 当客户端忽略ASP.NET Core中任何已启用的身份验证方案时,如何返回401 HTTP状态? - How to return 401 HTTP status when client omits any of enabled authentication schemes in ASP.NET Core? 如何从 Asp.Net Core View 组件返回错误的 HTTP 状态代码 - How to return HTTP Status Codes for errors from Asp.Net Core View Component 从ASP.NET Web API ASP.NET Core 2返回HTML并获取http状态406 - Return HTML from ASP.NET Web API ASP.NET Core 2 and get http status 406 如何在 asp.net core 2.1 中使用自定义消息设置状态代码? - How do I set the status code with custom message in asp.net core 2.1? 如何在 ASP.NET Core 3.1 中为 HTTP 状态 415 创建自定义响应? - How to create a custom response for HTTP status 415 in ASP.NET Core 3.1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM