Swashbuckle Swagger UI 不显示参数描述

Atl*_*oka 6 asp.net swagger swashbuckle

软件包版本6.0.0-beta902

例子 :

public class TestModel
{
    /// <summary>
    /// This is description of Name
    /// </summary>
    public string Name { get; set; }
}

public IActionResult HelloWorld(TestModel model)
{
    return Content("hello " + model.Name);
}
Run Code Online (Sandbox Code Playgroud)

为什么 Swagger UI 不显示 Name 参数说明?

Hel*_*eda 4

这通常是由于缺少配置引起的,请确保 IncludeXmlComments 具有 XML 文档的正确路径。

以下是在配置中包含所有 XML 文档的方法:

    public void ConfigureServices(IServiceCollection services)
    {
        // Add Swashbuckle
        services.AddSwaggerGen(options =>
        {
            options.DescribeAllEnumsAsStrings();
            options.SingleApiVersion(new Swashbuckle.Swagger.Model.Info()
            {
                Title = "My API",
                Version = "v1",
                Description = "My API",
                TermsOfService = "Terms Of Service"
            });

            //Set the comments path for the swagger json and ui.
            var basePath = PlatformServices.Default.Application.ApplicationBasePath;
            foreach (var name in Directory.GetFiles(basePath, "*.XML", SearchOption.AllDirectories))
            {
                options.IncludeXmlComments(name);
            }
        });
        // Add framework services.
        services.AddMvc();
    }
Run Code Online (Sandbox Code Playgroud)



这是正确显示的示例:

http://swashbuckletest.azurewebsites.net/swagger/ui/index?filter=TestEnum#/TestEnum/TestEnum_Put

其背后的代码位于 GitHub 上:

https://github.com/heldersepu/SwashbuckleTest/blob/master/Swagger_Test/Models/ViewModelTest.cs

  • 我确认,我的所有 xml 文件都已包含在内。Swagger 显示了我模型财产中的评论。但参数说明中却没有任何内容。 (2认同)