C# WebApi+Swagger

发布时间 2023-06-27 11:45:33作者: 一介桃白白

1、新建一个webapi项目

 

2、添加swagger的NuGet包

 

3、右键项目--》属性--》生成  输出勾选:XML文档文件,如果没有自动填充好路径,需要自己填写一下,文件名可以自己取

 

4、打开App_Start文件夹下的SwaggerConfig.cs文件,新增一个如下方法:

private static string GetXmlCommentsPath()
        {
            return System.String.Format(@"{0}\bin\MySwagger.xml", System.AppDomain.CurrentDomain.BaseDirectory);
        }

 

5、打开App_Start文件夹下的SwaggerConfig.cs文件,Register()方法新增一行代码 c.IncludeXmlComments(GetXmlCommentsPath());

 

6、新建一个webapi控制器

 

7、写一个测试接口,浏览swagger展示效果

public string[] getStr()
        {
            return new string[] { "value1", "value2" };
        }

 

 https://localhost:端口/swagger

 

 

 

 

 

 

 

参考地址:

https://www.cnblogs.com/zoujinhua/p/13552592.html