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

来源 https://www.javaroad.cn/questions/66058

 

ASP.NET Core APIs in the fast lane with Swagger and Autorest

Adding swagger in ASP.NET Core Web API

ASP.NET Core 1.0 MVC API documentation using Swashbuckle Swagger

 

一:

[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);
}

 

二:

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

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

三:

Swagger支持 ProducesResponseType 属性,它是MVC Web API Core属性,而不是Swagger . 与 SwaggerResponse 相同 .



 

上一篇:Swagger总结


下一篇:springboot整合~swagger~kafka~nginx~redis~mysql(在linux服务器环境下部署运行测试)