gpt4 book ai didi

c# - 使用 Swagger 向端点添加注释

转载 作者:行者123 更新时间:2023-12-05 05:06:41 36 4
gpt4 key购买 nike

我正在为我的 API 使用 Swagger/Swashbuckle。我希望 Swagger UI 显示方法描述。在他们的documents它说:


2 - 配置 Swashbuckle 以将文件上的 XML 注释合并到生成的 Swagger JSON 中:

services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1",
new Info
{
Title = "My API - V1",
Version = "v1"
}
);

var filePath = Path.Combine(System.AppContext.BaseDirectory, "MyApi.xml");
c.IncludeXmlComments(filePath);
}

有人可以解释一下吗?我应该用这段代码做什么?我是否将其复制并粘贴到某处?如果有,在哪里?

(.NET 框架 4.7)

编辑:

下面 Jawad 的回答让我找到了解决方案。在原始的 SwaggerConfig.cs 文件中,有这样的内容:

// If you annotate Controllers and API Types with
// Xml comments (http://msdn.microsoft.com/en-us/library/b2s063f7(v=vs.110).aspx), you can incorporate
// those comments into the generated docs and UI. You can enable this by providing the path to one or
// more Xml comment files.
//
//c.IncludeXmlComments(GetXmlCommentsPath());

我不清楚如何更改最后一行以添加我的 XML 文件。这有效:

c.IncludeXmlComments(Path.Combine(System.AppContext.BaseDirectory, "bin\\KGC.API.xml"));

我还必须添加 using System.IO

最佳答案

我的方法是更新 SwaggerConfig.cs 文件..

    public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;

GlobalConfiguration.Configuration
.EnableSwagger("docs/{apiVersion}", c =>
{
c.SingleApiVersion("v1", "Title Of API");
c.Schemes(new List<string> { "http", "https" });
c.UseFullTypeNameInSchemaIds();
c.IncludeXmlComments(Path.Combine(System.AppContext.BaseDirectory, "MyApi.xml"));
});
}

上面代码的最后一行启用了 XML 注释标记。

你必须做的另一件事是,

  1. 转到项目属性(不是解决方案)
  2. 构建/输出 -> 添加 XML 文档文件的路径。

供引用,this很有帮助。

关于c# - 使用 Swagger 向端点添加注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59688068/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com