如何在Swagger文档中包含框架类的摘要?

huangapple go评论69阅读模式
英文:

How to include summaries from the framework classes in the Swagger documentation?

问题

我知道如何使用为我的项目生成的XML文档文件,以便Swashbuckle将它们提供给Swagger文档,但是否有办法对标准框架类执行相同操作?例如,如果我的API引用了系统框架类型,我如何将为此类型定义的摘要和文档(通过IntelliSense可见)包含在Swagger文档中?

英文:

I know how to use the XML documentation files generated for my own projects, so that Swashbuckle feeds them to the Swagger documentation, but is there a way to do the same for the standard framework classes? For example, if my API references a system framework type, how do I get the summary and documentation defined for this type (and visible via IntelliSense) included in the Swagger documentation?

答案1

得分: 1

与包含您自己项目的XML文档在Swagger文档中的方式相同,您也可以包含.NET Framework或任何其他库的XML文档。

您需要找到库的XML文档文件(通常位于与DLL文件相同的文件夹中,具有相同的名称但扩展名为.xml),然后将其包含在Swagger配置中。

以下是如何在.NET Core或.NET 5+中执行此操作的步骤。首先,请确保框架DLL的XML文档文件存在。对于.NET Core或.NET 5+,这些文件通常位于NuGet包存储区(在Windows上是%userprofile%.nuget\packages,在基于Unix的系统上是~/.nuget/packages)。它们位于与DLL文件相同的目录中,名称相同但扩展名为.xml。

一旦您找到了XML文件,您可以在Startup.cs中将其添加到Swagger配置中,

public void ConfigureServices(IServiceCollection services)
{

    services.AddSwaggerGen(c =>
    {
        var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
        var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
        c.IncludeXmlComments(xmlPath);
        
        var frameworkXmlPath = @"<框架XML文件的路径>";
        c.IncludeXmlComments(frameworkXmlPath);
    });
}

请用实际的XML文档文件的路径替换<框架XML文件的路径>

如果您使用Docker或其他容器化/部署技术,请确保XML文件包含在部署包中。

PS:并非.NET框架中的所有类型都有文档,即使有文档,文档可能也不像您为自己的类型编写的文档那样全面。

英文:

The same way you include your own project's XML documentation in your Swagger documentation, you can also include XML documentation of the .NET Framework or any other library.

You need to find the XML documentation file for the library (it usually resides in the same folder as the DLL file, with the same name but with a .xml extension) and include it in your Swagger configuration.

Here's how you can do it for .NET Core or .NET 5+,First, ensure that the XML documentation file for the framework DLL is present. For .NET Core or .NET 5+, these files are usually located in the NuGet package store (%userprofile%.nuget\packages on Windows, ~/.nuget/packages on Unix-based systems). They are located in the same directory as the DLL files, with the same name but with an .xml extension.

Once you have located the XML file, you can add it to your Swagger configuration in your Startup.cs,

public void ConfigureServices(IServiceCollection services)
{

    services.AddSwaggerGen(c =&gt;
    {
        var xmlFile = $&quot;{Assembly.GetExecutingAssembly().GetName().Name}.xml&quot;;
        var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
        c.IncludeXmlComments(xmlPath);
        
        var frameworkXmlPath = @&quot;&lt;path-to-framework-xml-file&gt;&quot;;
        c.IncludeXmlComments(frameworkXmlPath);
    });
}

please replace <path-to-framework-xml-file> with the actual path to the XML documentation file for the framework DLL.

if you're using Docker or other containerisation/deployment techniques, make sure that the XML files are included in your deployment package.

PS: not all types in the .NET framework are documented, and even when they are, the documentation may not be as comprehensive as what you might write for your own types.

huangapple
  • 本文由 发表于 2023年7月28日 04:16:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定