C# 你是如何在这句话中加上一句夸夸其谈的话的;请求和响应模型;?
您可以像下面的示例那样在方法上添加注释,但是如何向请求和响应模型添加注释呢C# 你是如何在这句话中加上一句夸夸其谈的话的;请求和响应模型;?,c#,asp.net-core,swagger,swashbuckle,C#,Asp.net Core,Swagger,Swashbuckle,您可以像下面的示例那样在方法上添加注释,但是如何向请求和响应模型添加注释呢 /// <summary> /// my summary /// </summary> /// <remarks> /// remark goes here. /// </remarks> /// <param name="somepara">Required parameter: Example: </param> /// <return&g
/// <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>
//
///我的总结
///
///
///这里有一句话。
///
///必需参数:示例:
///返回注释
///嗯
我不确定这是否就是您所说的,但您可以像这样在不同的回复中添加注释
[SwaggerResponse(HttpStatusCode.Unauthorized, "Authorization has been denied for this request")]
这是用于修饰控制器方法的属性。是的,正如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; }
}
}
使用Swagger.Net.Annotations;
使用制度;
使用System.Collections.Generic;
Net系统;
使用System.Web.Http;
使用System.Web.Http.Results;
命名空间Swagger_Test.Controllers
{
公共类IHttpActionResultController:ApiController
{
[SwaggerResponse(HttpStatusCode.OK,“客户列表”,类型(IEnumerable))]
[SwaggerResponse(HttpStatusCode.NotFound,Type=typeof(NotFoundResult))]
公共IHttpActionResult Post(MyData数据)
{
抛出新的NotImplementedException();
}
}
///我的超级复制数据
公共类MyData
{
///唯一标识符
公共int id{get;set;}
///每个人都需要一个名字
公共字符串名称{get;set;}
}
}
大摇大摆的样子会像:
我使用的是.net core 3.0,因此除了@Helder的响应之外,我还必须执行以下两个步骤才能使XML注释可见 手动编辑项目文件
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
对于那些对现有答案不满意的人,请确保您的属性类所在的项目也启用了xml文档
在我的例子中,我有一个单独的DTO项目,需要将其添加到其中。请确保使用另一个
includexmlcomcomments
方法包含来自该项目的xml注释。下面是一个实例:下面是它背后的代码:这很有趣,我添加了///唯一标识符。。。就像在所示的示例中一样,但是描述没有显示在swagger文档中,我的模型在一个单独的项目中(这有关系吗?)这是一个bug!?如果您有外部模型,swagger将不会生成模型描述。添加响应注释也可以使用XML响应代码来完成。
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);
});