【发布时间】:2020-05-17 22:29:20
【问题描述】:
我正在编写一个 Web API,并定义了一个具有各种 GET、POST 方法等的控制器。我正在为我的文档使用 Swagger Open API,并希望了解正确的注释方法。这是我拥有的控制器方法的示例:
/// <summary>Download a file based on its Id.</summary>
/// <param name="id">Identity of file to download.</param>
/// <returns><see cref="MyFile" /> file content found.</returns>
[HttpGet("download/{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerResponse(200, "Myfile content", typeof(MyFile))]
[SwaggerResponse(404, "Could not find file", typeof(MyFile))]
public async Task<IActionResult> DownloadAsync(int id)
{
const string mimeType = "application/octet-stream";
var myFile = await _dbContext.MyFiles.FindAsync(id);
// If we cannot find the mapping, return 404.
if (myFile.IsNullOrDefault())
{
return NotFound();
}
// Download using file stream.
var downloadStream = await _blobStorage.DownloadBlob(myFile.FileLocation);
return new FileStreamResult(downloadStream, mimeType) { FileDownloadName = myFile.FileName };
}
如您所见,我同时使用 ProducesResponseType 和 SwaggerResponse 来描述下载方法。我对使用的正确属性有点困惑 - 招摇响应还是产生响应类型?我应该同时使用吗?为什么我会偏爱其中一个?
提前感谢您的任何指点! :)
【问题讨论】:
标签: c# asp.net-core asp.net-web-api swagger openapi