微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在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 相同 .



 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐