简体   繁体   English

如何在 Swagger for .Net Core Web Api 中覆盖描述“无参数”?

[英]How to override description 'No parameters' in Swagger for .Net Core Web Api?

In .Net Core Web Api project I have Get which doesn't need request data to be passed on.在 .Net Core Web Api 项目中,我有不需要传递请求数据的 Get。 By default SwaggerUi generates 'No parameters' description under Parameters tab.默认情况下,SwaggerUi 在“参数”选项卡下生成“无参数”描述。 See below.见下文。

在此处输入图片说明

My aim is to replace 'No parameters' string with something else.我的目标是用其他东西替换“无参数”字符串。 I'd like to add additional information why there is no need for parameters.我想添加其他信息,为什么不需要参数。 If it's possible, then please share a knowledge.如果可能,请分享知识。

I haven't found a way to modify the custom content of parameters after searching for a long time, but you can add the remarks you need on the url, like this:找了半天也没找到修改参数自定义内容的方法,不过你可以在url上加上你需要的备注,像这样:

For enabling XML comments, we need to do the following steps:要启用 XML 注释,我们需要执行以下步骤:

1.In the Build tab of the project properties, check the box labeled XML documentation file. 1. 在项目属性的 Build 选项卡中,选中标记为 XML 文档文件的框。 Let's keep the auto-generated file path.让我们保留自动生成的文件路径。

2.Suppress warning 1591, which will now give warnings about any method, class, or field that doesn't have triple-slash comments. 2.Suppress warning 1591,它现在将给出关于任何没有三斜线注释的方法、类或字段的警告。

在此处输入图片说明

In the ConfigureServices() method, configure Swagger to use the XML file that's generated in the above step:在 ConfigureServices() 方法中,配置 Swagger 以使用在上述步骤中生成的 XML 文件:

public void ConfigureServices(IServiceCollection services)
{
    // Register the Swagger generator, defining 1 or more Swagger documents
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo
       //......TODO

// 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);
    });

Now, adding triple-slash comments to the action method enhances the Swagger UI by adding a description to the section header.现在,向 action 方法添加三斜杠注释可以通过向节标题添加描述来增强 Swagger UI。

 /// <summary>
        /// This is test    
        /// </summary>
        /// 
        /// 
       [Route("test")]
       [HttpGet]
        public string Test()
        {
            return "v1 test";
        }

Result:结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM