Net Core教程

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

本文主要是介绍如何在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 相同 .


 

这篇关于如何在ASP.NET Core中返回自定义HTTP状态/消息而不返回对象,IActionResult等?的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!