001*_*001 3 c# swagger swashbuckle asp.net-core
您可以在类似于下面示例的方法上添加注释,但是如何在请求和响应模型中添加注释呢?
/// <summary>
/// my summary
/// </summary>
/// <remarks>
/// remark goes here.
/// </remarks>
/// <param name="somepara">Required parameter: Example: </param>
/// <return>Returns comment</return>
/// <response code="200">Ok</response>
Run Code Online (Sandbox Code Playgroud)
Din*_*nch 10
我使用的是 .net core 3.0,所以除了@Helder 的响应之外,我还必须执行以下两个步骤才能使 XML 注释可见。
手动编辑项目文件。
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
将下面添加到 startup.cs 服务配置方法。
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "My Good API",
Version = "v1",
Description = "Doesn't hurt to add some description."
});
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
Run Code Online (Sandbox Code Playgroud)
是的,就像Dimitar所说的那样,您可以使用SwaggerResponse将注释添加到响应中,请求有些不同,就像您将xml注释添加到操作中一样,您应该添加到参数中,下面是一个示例:
using Swagger.Net.Annotations;
using System;
using System.Collections.Generic;
using System.Net;
using System.Web.Http;
using System.Web.Http.Results;
namespace Swagger_Test.Controllers
{
public class IHttpActionResultController : ApiController
{
[SwaggerResponse(HttpStatusCode.OK, "List of customers", typeof(IEnumerable<int>))]
[SwaggerResponse(HttpStatusCode.NotFound, Type = typeof(NotFoundResult))]
public IHttpActionResult Post(MyData data)
{
throw new NotImplementedException();
}
}
/// <summary>My super duper data</summary>
public class MyData
{
/// <summary>The unique identifier</summary>
public int id { get; set; }
/// <summary>Everyone needs a name</summary>
public string name { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12104 次 |
| 最近记录: |