Part1 记录mvc api 简单使用swagger
1、创建mvc api 项目
2、初始化包 (工具-NuGet包管理器-管理解决方案的NuGet包) -Swashbuckle
3、在App_Start 生成SwaggerConfig.cs 文件 修改SwaggerConfig 文件如下 :
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "API Test");
c.IncludeXmlComments(GetXmlCommentsPath());
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
})
.EnableSwaggerUi(c =>
{
});
}
private static string GetXmlCommentsPath()
{
return $@"{System.AppDomain.CurrentDomain.BaseDirectory}\bin\WebAppSwagger.XML";
}
}
5、配置生成的xml 文件路径 也就是方法GetXmlCommentsPath() 所指定的路径
6、运行程序,地址栏输入http://localhost:2122/swagger/ui/index 成功显示
7、我们可以在此基础上扩展swagger的css、js、html
在项目中新建个文件夹SwaggerExtensions用于存放扩展文件 ,在这个文件夹下添加一个css文件:swagger.css,我们这里只是修改了一下头部的颜色