基于。NetCore开发博客项目StarBlog(26)集成
1前言
这是StarBlog系列在2023年的第一篇更新
在之前的文章里,我们已经完成了部分接口的开发,接下来需要使用curl、Postman这类工具对这些接口进行测试,但接口一多,每次测试都要一个个填入地址和对应参数会比较麻烦
我们需要一种直观的方式来汇总项目里的所有接口,并且如果能直接在里面调试接口,那就更好了。
Swagger:诶嘿,说的不就是我吗?2Swagger介绍
来一段官网的介绍SimplifyAPIdevelopmentforusers,teams,andenterpriseswiththeSwaggeropensourceandprofessionaltoolset。
翻译:Swagger是开源和专业的工具集,可以简化用户、团队和企业的API开发。
一般来说,swagger用起来有两部分,一个是OpenAPI一个是SwaggerUI
在Swagger官网上,OpenAPI介绍得天花乱坠TheOpenAPISpecification,formerlyknownastheSwaggerSpecification,istheworld’sstandardfordefiningRESTfulinterfaces。TheOASenablesdeveloperstodesignatechnologyagnosticAPIinterfacethatformsthebasisoftheirAPIdevelopmentandconsumption。
翻译:OpenAPI规范,以前称为Swagger规范,是定义RESTful接口的世界标准。OAS使开发人员能够设计一个与技术无关的API接口,该接口构成了他们API开发和使用的基础。
简单说OpenAPI是个标准,需要每种语言和框架自行实现一个工具,用来把项目里的接口都整合起来,生成swagger。json文件
然后SwaggerUI就是个网页,读取这个swagger。json就可以把所有接口以及参数显示出来,还可以很方便调试,效果如图。image3Swashbuckle。AspNetCore
前面说到每种框架都要自己实现一个工具来生成swagger。json,这个Swashbuckle。AspNetCore就是。NetCore平台的实现,用就完事了。
项目主页:https:github。comdomaindrivendevSwashbuckle。AspNetCore
Tips:如果是创建WebApi项目,代码模板里面默认就有Swagger了,不用手动添加。
StarBlog项目一开始是使用MVC模板,所以没有自带Swagger,需要手动添加。
直接使用nuget添加Swashbuckle。AspNetCore这个包就完事了。
这个包功能很多,内置了SwaggerUI这个官方界面,还有一个ReDoc的纯静态接口文档网页(这个ReDoc只能看接口不能调试)。4初步使用
为了保证Program。cs代码整洁,我们在StarBlog。WebExtensions里面创建ConfigureSwagger类spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;staticspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;classspanspanstylecolor:a6e22e;fontweight:bold;lineheight:26px;ConfigureSwaggerspan{
spanstylelineheight:26px;spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;staticspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;voidspanspanstylecolor:a6e22e;fontweight:bold;lineheight:26px;AddSwaggerspan(spanstylelineheight:26px;spanstylecolor:f92672;fontweight:bold;lineheight:26px;thisspanIServiceCollectionservicesspan)span{
services。AddSwaggerGen(options{
options。SwaggerDoc(spanstylecolor:a6e22e;lineheight:26px;v1span,spanstylecolor:f92672;fontweight:bold;lineheight:26px;newspanOpenApiInfo{Versionspanstylecolor:a6e22e;lineheight:26px;v1span,Titlespanstylecolor:a6e22e;lineheight:26px;APIsspan});
spanstylecolor:75715e;lineheight:26px;在接口文档上显示XML注释span
spanstylecolor:f92672;fontweight:bold;lineheight:26px;varspanfilePathPath。Combine(System。AppContext。BaseDirectory,spanstylecolor:a6e22e;lineheight:26px;spanstylelineheight:26px;{spanstylecolor:f92672;fontweight:bold;lineheight:26px;typeofspan(Program)。Assembly。GetName()。Name}span。xmlspan);
options。IncludeXmlComments(filePath,spanstylecolor:f92672;fontweight:bold;lineheight:26px;truespan);
});
}
spanstylelineheight:26px;spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;staticspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;voidspanspanstylecolor:a6e22e;fontweight:bold;lineheight:26px;UseSwaggerPkgspan(spanstylelineheight:26px;spanstylecolor:f92672;fontweight:bold;lineheight:26px;thisspanIApplicationBuilderappspan)span{
app。UseSwagger();
app。UseSwaggerUI(options{
options。RoutePrefixspanstylecolor:a6e22e;lineheight:26px;apidocsswaggerspan;
options。SwaggerEndpoint(spanstylecolor:a6e22e;lineheight:26px;swaggerv1swagger。jsonspan,spanstylecolor:a6e22e;lineheight:26px;APIsspan);
});
app。UseReDoc(options{
options。RoutePrefixspanstylecolor:a6e22e;lineheight:26px;apidocsredocspan;
options。SpecUrlspanstylecolor:a6e22e;lineheight:26px;swaggerv1swagger。jsonspan;
});
}
}
上面代码可以看到有三步AddSwaggerGen对应前文说的生成swagger。jsonUseSwagger让浏览器可以访问到swaggerv1swagger。json这类路径UseSwaggerUI提供SwaggerUI的网页访问
然后回到Program。cs里面,分别注册服务和添加中间件就好了。spanstylecolor:75715e;lineheight:26px;注册服务span
builder。Services。AddSwagger();
spanstylecolor:75715e;lineheight:26px;添加中间件span
app。UseSwaggerPkg();
现在启动项目,访问http:〔本地地址〕apidocsswagger就能看到接口文档了
效果大概这样image扩展:关于XML注释
C的代码注释可以导出XML,然后显示在swagger文档上
注意需要手动在。csproj项目配置里面开启,才会输出XML文档spanstylecolor:75715e;lineheight:26px;!输出XMLspan
spanstylecolor:f92672;lineheight:26px;spanstylelineheight:26px;PropertyGroupspanspan
spanstylecolor:f92672;lineheight:26px;spanstylelineheight:26px;GenerateDocumentationFilespanspantruespanstylecolor:f92672;lineheight:26px;spanstylelineheight:26px;GenerateDocumentationFilespanspan
spanstylecolor:f92672;lineheight:26px;spanstylelineheight:26px;NoWarnspanspan(NoWarn);1591spanstylecolor:f92672;lineheight:26px;spanstylelineheight:26px;NoWarnspanspan
spanstylecolor:f92672;lineheight:26px;spanstylelineheight:26px;PropertyGroupspanspan
但是开启XML之后,IDE很蠢的要求我们所有public成员都写上注释,很烦,加上NoWarn(NoWarn);1591NoWarn这行就可以关掉这个警告。
在Swagger里加载XML文档,既可以用本文前面写的方式spanstylecolor:f92672;fontweight:bold;lineheight:26px;varspanfilePathPath。Combine(System。AppContext。BaseDirectory,spanstylecolor:a6e22e;lineheight:26px;spanstylelineheight:26px;{spanstylecolor:f92672;fontweight:bold;lineheight:26px;typeofspan(Program)。Assembly。GetName()。Name}span。xmlspan);
options。IncludeXmlComments(filePath,spanstylecolor:f92672;fontweight:bold;lineheight:26px;truespan);
还可以用第二种,加载目录里的全部XMLspanstylecolor:f92672;fontweight:bold;lineheight:26px;varspanxmlFilesDirectory。GetFiles(AppContext。BaseDirectory,spanstylecolor:a6e22e;lineheight:26px;。xmlspan);
spanstylecolor:f92672;fontweight:bold;lineheight:26px;foreachspan(spanstylecolor:f92672;fontweight:bold;lineheight:26px;varspanfilespanstylecolor:f92672;fontweight:bold;lineheight:26px;inspanxmlFiles){
options。IncludeXmlComments(file,spanstylecolor:f92672;fontweight:bold;lineheight:26px;truespan);
}
具体用哪种,都行吧,看心情扩展:关于AddEndpointsApiExplorer
在AddSwagger扩展方法这里可能有同学会有疑问
为啥创建。Net6项目后默认是这两行代码builder。Services。AddEndpointsApiExplorer();
builder。Services。AddSwaggerGen();
而我这里只有一行代码services。AddSwaggerGen();
先说结论:AddEndpointsApiExplorer是为了支持MinimalApi的。
因为StarBlog项目使用的是MVC模板,在Program。cs的最开始可以看到这行代码,添加控制器和视图builder。Services。AddControllersWithViews();
翻一下这个框架的源码,可以看到这个方法的套娃是这样的AddControllersWithViews()AddControllersWithViewsCore()AddControllersCore()
而在AddControllersCore里面,又调用了AddApiExplorerspanstylelineheight:26px;spanstylecolor:f92672;fontweight:bold;lineheight:26px;privatespanspanstylecolor:f92672;fontweight:bold;lineheight:26px;staticspanIMvcCoreBuilderspanstylecolor:a6e22e;fontweight:bold;lineheight:26px;AddControllersCorespan(spanstylelineheight:26px;IServiceCollectionservicesspan)span{
spanstylecolor:75715e;lineheight:26px;Thismethodexcludesalloftheviewrelatedservicesbydefault。span
spanstylecolor:f92672;fontweight:bold;lineheight:26px;varspanbuilderservices
。AddMvcCore()
。AddApiExplorer()
。AddAuthorization()
。AddCors()
。AddDataAnnotations()
。AddFormatterMappings();
spanstylecolor:f92672;fontweight:bold;lineheight:26px;ifspan(MetadataUpdater。IsSupported){
services。TryAddEnumerable(
ServiceDescriptor。SingletonIActionDescriptorChangeProvider,HotReloadService());
}
spanstylecolor:f92672;fontweight:bold;lineheight:26px;returnspanbuilder;
}
就是说正常的项目已经有ApiExplorer这个东西了,但是MinimalApi项目没有,所以本项目不需要builder。Services。AddEndpointsApiExplorer();这行代码。
详情可以阅读参考资料的第一个链接。5接口分组
接口文档有了,但项目里接口太多了,几十个接口全挤在一个页面上,找都找得眼花了
这时候可以给接口分个组
先来给StarBlog项目里面的接口分个类,根据不同用途,大致分成这五类:admin管理员相关接口common通用公共接口auth授权接口blog博客管理接口test测试接口
还是在上面那个ConfigureSwagger。cs文件
修改AddSwagger方法,把这几个分组添加进去services。AddSwaggerGen(options{
options。SwaggerDoc(spanstylecolor:a6e22e;lineheight:26px;adminspan,spanstylecolor:f92672;fontweight:bold;lineheight:26px;newspanOpenApiInfo{
Versionspanstylecolor:a6e22e;lineheight:26px;v1span,
Titlespanstylecolor:a6e22e;lineheight:26px;AdminAPIsspan,
Descriptionspanstylecolor:a6e22e;lineheight:26px;管理员相关接口span
});
options。SwaggerDoc(spanstylecolor:a6e22e;lineheight:26px;commonspan,spanstylecolor:f92672;fontweight:bold;lineheight:26px;newspanOpenApiInfo{
Versionspanstylecolor:a6e22e;lineheight:26px;v1span,
Titlespanstylecolor:a6e22e;lineheight:26px;CommonAPIsspan,
Descriptionspanstylecolor:a6e22e;lineheight:26px;通用公共接口span
});
options。SwaggerDoc(spanstylecolor:a6e22e;lineheight:26px;authspan,spanstylecolor:f92672;fontweight:bold;lineheight:26px;newspanOpenApiInfo{
Versionspanstylecolor:a6e22e;lineheight:26px;v1span,
Titlespanstylecolor:a6e22e;lineheight:26px;AuthAPIsspan,
Descriptionspanstylecolor:a6e22e;lineheight:26px;授权接口span
});
options。SwaggerDoc(spanstylecolor:a6e22e;lineheight:26px;blogspan,spanstylecolor:f92672;fontweight:bold;lineheight:26px;newspanOpenApiInfo{
Versionspanstylecolor:a6e22e;lineheight:26px;v1span,
Titlespanstylecolor:a6e22e;lineheight:26px;BlogAPIsspan,
Descriptionspanstylecolor:a6e22e;lineheight:26px;博客管理接口span
});
options。SwaggerDoc(spanstylecolor:a6e22e;lineheight:26px;testspan,spanstylecolor:f92672;fontweight:bold;lineheight:26px;newspanOpenApiInfo{
Versionspanstylecolor:a6e22e;lineheight:26px;v1span,
Titlespanstylecolor:a6e22e;lineheight:26px;TestAPIsspan,
Descriptionspanstylecolor:a6e22e;lineheight:26px;测试接口span
});
});
这样就会生成五个swagger。json文件,路径分别是swaggeradminswagger。jsonswaggercommonswagger。jsonswaggerauthswagger。jsonswaggerblogswagger。jsonswaggertestswagger。json
所以下面的UseSwaggerPkg方法也要对应修改spanstylelineheight:26px;spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;staticspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;voidspanspanstylecolor:a6e22e;fontweight:bold;lineheight:26px;UseSwaggerPkgspan(spanstylelineheight:26px;spanstylecolor:f92672;fontweight:bold;lineheight:26px;thisspanIApplicationBuilderappspan)span{
app。UseSwagger();
app。UseSwaggerUI(options{
options。RoutePrefixspanstylecolor:a6e22e;lineheight:26px;apidocsswaggerspan;
options。SwaggerEndpoint(spanstylecolor:a6e22e;lineheight:26px;swaggeradminswagger。jsonspan,spanstylecolor:a6e22e;lineheight:26px;Adminspan);
options。SwaggerEndpoint(spanstylecolor:a6e22e;lineheight:26px;swaggerblogswagger。jsonspan,spanstylecolor:a6e22e;lineheight:26px;Blogspan);
options。SwaggerEndpoint(spanstylecolor:a6e22e;lineheight:26px;swaggerauthswagger。jsonspan,spanstylecolor:a6e22e;lineheight:26px;Authspan);
options。SwaggerEndpoint(spanstylecolor:a6e22e;lineheight:26px;swaggercommonswagger。jsonspan,spanstylecolor:a6e22e;lineheight:26px;Commonspan);
options。SwaggerEndpoint(spanstylecolor:a6e22e;lineheight:26px;swaggertestswagger。jsonspan,spanstylecolor:a6e22e;lineheight:26px;Testspan);
});
}
接下来,要让Swagger知道每个接口都是属于哪个分组的。
具体方法是在Controller上添加ApiExplorerSettings特性。
比如BlogController是属于blog分组,在class定义前面添加一行代码〔spanstylecolor:75715e;lineheight:26px;ApiExplorerSettings(GroupNamespanstylelineheight:26px;blogspan)span〕
spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;classspanspanstylecolor:a6e22e;fontweight:bold;lineheight:26px;BlogControllerspan:spanstylecolor:a6e22e;fontweight:bold;lineheight:26px;ControllerBasespan{
spanstylecolor:75715e;lineheight:26px;。。。span
}
其他的Controller也是类似的操作,具体分组跟StarBlog。WebApis下的目录结构一样,这里就不赘述了。实现效果
做完之后,打开swagger接口文档页面
可以看到右上角可以选择接口分组了image
搞定。6优化分组
前文对于Swagger分组的实现其实是一种硬编码,不同分组的Controller上面需要加上〔ApiExplorerSettings(GroupNameblog)〕特性,分组名全靠复制粘贴,在项目比较小的情况下还好,如果分组多起来了,有几百个接口的时候,估计人就麻了吧Q:你刚才干嘛不早说
A:循序渐进嘛
A:StarBlog项目也是最近才换到新版分组的
在StarBlog。WebModels里添加个新的类SwaggerGroupspanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;classspanspanstylecolor:a6e22e;fontweight:bold;lineheight:26px;SwaggerGroupspan{
spanstylecolor:75715e;lineheight:26px;spanstylefontweight:bold;lineheight:26px;spanspanstylefontweight:bold;lineheight:26px;summaryspanspan
spanstylecolor:75715e;lineheight:26px;spanstylefontweight:bold;lineheight:26px;span组名称(同时用于做URL前缀)span
spanstylecolor:75715e;lineheight:26px;spanstylefontweight:bold;lineheight:26px;spanspanstylefontweight:bold;lineheight:26px;summaryspanspan
spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;stringspanName{spanstylecolor:f92672;fontweight:bold;lineheight:26px;getspan;spanstylecolor:f92672;fontweight:bold;lineheight:26px;setspan;}
spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;stringspan?Title{spanstylecolor:f92672;fontweight:bold;lineheight:26px;getspan;spanstylecolor:f92672;fontweight:bold;lineheight:26px;setspan;}
spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;stringspan?Description{spanstylecolor:f92672;fontweight:bold;lineheight:26px;getspan;spanstylecolor:f92672;fontweight:bold;lineheight:26px;setspan;}
spanstylelineheight:26px;spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:a6e22e;fontweight:bold;lineheight:26px;SwaggerGroupspan(spanstylelineheight:26px;spanstylecolor:f92672;fontweight:bold;lineheight:26px;stringspanname,spanstylecolor:f92672;fontweight:bold;lineheight:26px;stringspan?titlespanstylecolor:f92672;fontweight:bold;lineheight:26px;span,spanstylecolor:f92672;fontweight:bold;lineheight:26px;stringspan?descriptionspanstylecolor:f92672;fontweight:bold;lineheight:26px;spanspan)span{
Namename;
Titletitle;
Descriptiondescription;
}
spanstylecolor:75715e;lineheight:26px;spanstylefontweight:bold;lineheight:26px;spanspanstylefontweight:bold;lineheight:26px;summaryspanspan
spanstylecolor:75715e;lineheight:26px;spanstylefontweight:bold;lineheight:26px;span生成spanstylefontweight:bold;lineheight:26px;seecrefMicrosoft。OpenApi。Models。OpenApiInfospanspan
spanstylecolor:75715e;lineheight:26px;spanstylefontweight:bold;lineheight:26px;spanspanstylefontweight:bold;lineheight:26px;summaryspanspan
spanstylelineheight:26px;spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanOpenApiInfospanstylecolor:a6e22e;fontweight:bold;lineheight:26px;ToOpenApiInfospan(spanstylelineheight:26px;spanstylecolor:f92672;fontweight:bold;lineheight:26px;stringspanversionspanstylecolor:a6e22e;lineheight:26px;1。0spanspan)span{
spanstylecolor:f92672;fontweight:bold;lineheight:26px;varspanitemspanstylecolor:f92672;fontweight:bold;lineheight:26px;newspanOpenApiInfo();
Title??Name;
Description??Name;
spanstylecolor:f92672;fontweight:bold;lineheight:26px;returnspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;newspanOpenApiInfo{TitleTitle,DescriptionDescription,Versionversion};
}
}
然后改造一下StarBlog。WebExtensionsConfigureSwagger。cs
在这个文件里面添加个新的类,这样就不会硬编码了spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;staticspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;classspanspanstylecolor:a6e22e;fontweight:bold;lineheight:26px;ApiGroupsspan{
spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;constspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;stringspanAdminspanstylecolor:a6e22e;lineheight:26px;adminspan;
spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;constspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;stringspanAuthspanstylecolor:a6e22e;lineheight:26px;authspan;
spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;constspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;stringspanCommonspanstylecolor:a6e22e;lineheight:26px;commonspan;
spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;constspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;stringspanBlogspanstylecolor:a6e22e;lineheight:26px;blogspan;
spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;constspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;stringspanTestspanstylecolor:a6e22e;lineheight:26px;testspan;
}
在ConfigureSwagger里添加一些代码,创建SwaggerGroup列表spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;staticspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;classspanspanstylecolor:a6e22e;fontweight:bold;lineheight:26px;ConfigureSwaggerspan{
spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;staticspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;readonlyspanListSwaggerGroupGroupsspanstylecolor:f92672;fontweight:bold;lineheight:26px;newspan(){
spanstylecolor:f92672;fontweight:bold;lineheight:26px;newspanSwaggerGroup(ApiGroups。Admin,spanstylecolor:a6e22e;lineheight:26px;AdminAPIsspan,spanstylecolor:a6e22e;lineheight:26px;管理员相关接口span),
spanstylecolor:f92672;fontweight:bold;lineheight:26px;newspanSwaggerGroup(ApiGroups。Auth,spanstylecolor:a6e22e;lineheight:26px;AuthAPIsspan,spanstylecolor:a6e22e;lineheight:26px;授权接口span),
spanstylecolor:f92672;fontweight:bold;lineheight:26px;newspanSwaggerGroup(ApiGroups。Common,spanstylecolor:a6e22e;lineheight:26px;CommonAPIsspan,spanstylecolor:a6e22e;lineheight:26px;通用公共接口span),
spanstylecolor:f92672;fontweight:bold;lineheight:26px;newspanSwaggerGroup(ApiGroups。Blog,spanstylecolor:a6e22e;lineheight:26px;BlogAPIsspan,spanstylecolor:a6e22e;lineheight:26px;博客管理接口span),
spanstylecolor:f92672;fontweight:bold;lineheight:26px;newspanSwaggerGroup(ApiGroups。Test,spanstylecolor:a6e22e;lineheight:26px;TestAPIsspan,spanstylecolor:a6e22e;lineheight:26px;测试接口span)
};
}
然后把后面的AddSwagger方法改成这样,那一坨东西,现在一行代码就代替了spanstylelineheight:26px;spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;staticspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;voidspanspanstylecolor:a6e22e;fontweight:bold;lineheight:26px;AddSwaggerspan(spanstylelineheight:26px;spanstylecolor:f92672;fontweight:bold;lineheight:26px;thisspanIServiceCollectionservicesspan)span{
services。AddSwaggerGen(options{
Groups。ForEach(spanstylecolor:f92672;fontweight:bold;lineheight:26px;groupspanoptions。SwaggerDoc(spanstylecolor:f92672;fontweight:bold;lineheight:26px;groupspan。Name,spanstylecolor:f92672;fontweight:bold;lineheight:26px;groupspan。ToOpenApiInfo()));
spanstylecolor:75715e;lineheight:26px;XML注释span
spanstylecolor:f92672;fontweight:bold;lineheight:26px;varspanfilePathPath。Combine(AppContext。BaseDirectory,spanstylecolor:a6e22e;lineheight:26px;spanstylelineheight:26px;{spanstylecolor:f92672;fontweight:bold;lineheight:26px;typeofspan(Program)。Assembly。GetName()。Name}span。xmlspan);
options。IncludeXmlComments(filePath,spanstylecolor:f92672;fontweight:bold;lineheight:26px;truespan);
});
}
接着是UseSwaggerPkg方法,简单spanstylelineheight:26px;spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;staticspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;voidspanspanstylecolor:a6e22e;fontweight:bold;lineheight:26px;UseSwaggerPkgspan(spanstylelineheight:26px;spanstylecolor:f92672;fontweight:bold;lineheight:26px;thisspanIApplicationBuilderappspan)span{
app。UseSwagger();
app。UseSwaggerUI(opt{
opt。RoutePrefixspanstylecolor:a6e22e;lineheight:26px;apidocsswaggerspan;
spanstylecolor:75715e;lineheight:26px;分组span
Groups。ForEach(spanstylecolor:f92672;fontweight:bold;lineheight:26px;groupspanopt。SwaggerEndpoint(spanstylecolor:a6e22e;lineheight:26px;swaggerspanstylelineheight:26px;{spanstylecolor:f92672;fontweight:bold;lineheight:26px;groupspan。Name}spanswagger。jsonspan,spanstylecolor:f92672;fontweight:bold;lineheight:26px;groupspan。Name));
});
}
Controller里面也对应修改成这样〔spanstylecolor:75715e;lineheight:26px;ApiExplorerSettings(GroupNameApiGroups。Blog)span〕
spanstylecolor:f92672;fontweight:bold;lineheight:26px;publicspanspanstylecolor:f92672;fontweight:bold;lineheight:26px;classspanspanstylecolor:a6e22e;fontweight:bold;lineheight:26px;BlogControllerspan:spanstylecolor:a6e22e;fontweight:bold;lineheight:26px;ControllerBasespan{
}
完美7小结
Swagger之大,一锅炖不下
关于Swagger还有其他的用法,但需要一些前置知识,因此本文不会把StarBlog项目中关于Swagger的部分全部介绍完
等把相关的前置知识写完,再来完善对应的用法
这也跟StarBlog的开发过程是吻合的8参考资料https:stackoverflow。comquestions71932980whatisaddendpointsapiexplorerinaspnetcore69系列文章基于。NetCore开发博客项目StarBlog(1)为什么需要自己写一个博客?基于。NetCore开发博客项目StarBlog(2)环境准备和创建项目基于。NetCore开发博客项目StarBlog(3)模型设计基于。NetCore开发博客项目StarBlog(4)markdown博客批量导入基于。NetCore开发博客项目StarBlog(5)开始搭建Web项目基于。NetCore开发博客项目StarBlog(6)页面开发之博客文章列表基于。NetCore开发博客项目StarBlog(7)页面开发之文章详情页面基于。NetCore开发博客项目StarBlog(8)分类层级结构展示基于。NetCore开发博客项目StarBlog(9)图片批量导入基于。NetCore开发博客项目StarBlog(10)图片瀑布流基于。NetCore开发博客项目StarBlog(11)实现访问统计基于。NetCore开发博客项目StarBlog(12)Razor页面动态编译基于。NetCore开发博客项目StarBlog(13)加入友情链接功能基于。NetCore开发博客项目StarBlog(14)实现主题切换功能基于。NetCore开发博客项目StarBlog(15)生成随机尺寸图片基于。NetCore开发博客项目StarBlog(16)一些新功能(监控统计配置初始化)基于。NetCore开发博客项目StarBlog(17)自动下载文章里的外部图片基于。NetCore开发博客项目StarBlog(18)实现本地Typora文章打包上传基于。NetCore开发博客项目StarBlog(19)Markdown渲染方案探索基于。NetCore开发博客项目StarBlog(20)图片显示优化基于。NetCore开发博客项目StarBlog(21)开始开发RESTFul接口基于。NetCore开发博客项目StarBlog(22)开发博客文章相关接口基于。NetCore开发博客项目StarBlog(23)文章列表接口分页、过滤、搜索、排序基于。NetCore开发博客项目StarBlog(24)统一接口数据返回格式基于。NetCore开发博客项目StarBlog(25)图片接口与文件上传基于。NetCore开发博客项目StarBlog(26)集成Swagger接口文档