游戏电视苹果数码历史美丽
投稿投诉
美丽时装
彩妆资讯
历史明星
乐活安卓
数码常识
驾车健康
苹果问答
网络发型
电视车载
室内电影
游戏科学
音乐整形

基于。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接口文档

良辰好景知几何多编剧联合创作把观众写晕多编剧联合创作把观众写晕近期播出的年代剧《良辰好景知几何》,讲述性情桀骜不驯的将门之子萧北辰和落魄千金林杭景历经几重误会、波折,因爱蜕变成长,在烽火年代踏浪而行的故事。该……不学郭艾伦?孙铭徽过犹不及,失误葬送比赛,终究辜负杜锋的信任亚洲杯四分之一决赛,中国队面对黎巴嫩队,以69比72遗憾告负,再次打出了一场历史级败仗:亚洲杯赛场第一次输给对手。巧合的是中国男篮此前唯一一次输给对手的比赛也是杜锋挂帅,连续打……学丁宁还是学张继科?刘诗雯职业生涯已基本结束,退役其实存变数在国乒女队中,刘诗雯年龄不占优势,体能不占优势,竞技状态也不占优势,当然这是与国乒顶尖选手相比,如果她想继续打比赛,实力一样很强,但却已经无法代表国乒女队核心战力,况且排名也早……为啥睡觉不能脚朝西,头朝东?告诉你原因,或许并非迷信人每天都要进行睡眠,睡眠充足能够使身心得到放松,缓解身体的疲惫感,还能够保持精神状态充足,对于身体尤为重要。现在大家的养生意识逐渐提高,对于睡觉这件事,有睡觉不能脚朝西头……蒋光太不是山东泰山队的菜,到泰山极大可能踢不上球国家队12强赛正在进行,归化国脚的归宿又让球迷们闹心了,网上传播着各种版本,说的最多的是蒋光太由山东泰山收留,现在的山东泰山队中有王彤,刘军帅,戴琳等正牌的中后卫,还有郑铮也可……巴黎奥运敲定删减举重!增加美国4大优势项目,中国举重队遭重创巴黎奥运敲定!删减举重4枚金牌项目,增加美国4大优势项目12月21日据国际举联消息,在乌兹别克斯坦塔什干召开大会之后,下一届巴黎奥运会举重级别被敲定。国际举联执委、俄罗斯……NBA四支球队将重建!西部3豪强榜上有名,76人进入高危期休赛期NBA各队都开始了忙碌的选人交易,而各支强队都在筹划补强,试图冲击下赛季的总冠军,而这四支球队如果下赛季战绩仍然无法取得突破,重建就将迫在眉睫。第一位洛杉矶湖人,可……新金主为申花敲定重量级引援,曾在欧冠劲旅效力,单赛季独造15日前,根据外媒报道称一家中超俱乐部正在跟喀麦隆前锋恩库杜进行接触。恩库杜是一位当打之年的王牌前锋,曾经在法甲豪门马赛效力,并且有可圈可点的表现,单赛季为球队打入了10个进球,同……一切舆论矛头指向腾讯?毛星云跳楼后,网友声讨正义却被误导要说近期游戏圈引发热议的话题,肯定是腾讯游戏开发大神毛星云的离世了,这一事件的发生吸引了不少网友的关注。而在可惜这位天才开发者英年早逝的同时,网络上也出现了各种揣测与推断。……人民币贬值了对你到底有什么影响?截至2022年11月3号人民币兑美元已经到了7。3:1,最近这波贬值是从2022年4月开始的,在半年的时间里人民币兑美元从6。3跌到7。3。先说说汇率是怎么回事货币……智能科技满满哒三屏一带互动(仪表盘、中央娱乐系统、ARHUD、ID。Light):通过E3架构,实现5。3英寸数字仪表、12英寸中控屏、ARHUD、ID。Light互动灯带的整体互动,带给用……美女泛滥,懒汉光棍遍地!如今,横店20万群演的归宿到底在哪?如果问这个世界上什么职业既光鲜亮丽又日进斗金,演员无疑是一个最好的答案。前有刘姓女艺人五年替丈夫还债4个亿的新闻,后有某爽日薪208万的爆料。毫不夸张地说,顶流演员……
一加6T成功运行Windows安装环境感谢IT之家网友wherewhere的线索投递!IT之家3月31日消息继此前在Pixel3XL手机上运行Windows10系统后,开发者NTAuthority今日又晒出了……带孩子的老人和不带孩子的老人,晚年生活差距大,究竟谁更幸福?本文由兜妈爱叨叨原创,版权所有,侵权必究在很多人的认知中,老人帮助子女带孩子是一件非常普遍且理所应当的事情,毕竟如果条件允许,大多数父母都不会愿意看着自己的孩子被压力裹挟……三星Flex等新商标曝光!可折叠手机的味道IT之家11月5日消息此前一系列消息显示,三星即将带来一款可折叠手机。今日IT之家曾报道了,三星官方推特的头像已经更换称了弯曲的Samsung字样,或是在暗示这款手机即将到来。……诺基亚8Sirocco国际版开始推送安卓9Pie全新导航栏电感谢IT之家网友贫道小何的线索投递!IT之家1月26日消息今天凌晨有IT之家网友在IT圈投稿反馈,诺基亚8Sirocco国际版已经开始推送官方的安卓9Pie系统更新,更新……山东高速,调整加速主教练王晗上任后推出一个系列制度,进行了大刀阔斧的改革,对徐长锁时期的老臣进行了清洗,毕竟去年老徐把一把好牌打了个稀烂,在12进8的比赛中被深圳队2:0横扫!目前,训练仍……诺基亚5(2017)安卓9Pie升级已开始推送IT之家1月24日消息就在刚才,HMDGlobal的CPOJuhoSarvikas在推特上宣布,第一代诺基亚5手机的安卓9Pie系统已经开始推送。几天前,HMD刚公开了诺……视觉简化谷歌变更AndroidOne标志设计IT之家2月26日消息据台媒新闻,在之前HMDGlobal揭晓的诺基亚系列新机中,可以发现谷歌悄悄变更了其AndroidOne品牌标志设计,改为更精简的白底绿底,以及绿色And……教人自杀禁书重返网络?谁该来担责?近日,一本教人自杀的图书再次出现网络引发关注在相关售卖平台上商品表面上是印有自杀封面的空白笔记本实际售出的笔记本里却有文字内容孩子在电……DxOMark公布荣耀V30PRO手机主摄得分122分,位列IT之家1月17日消息今天下午,DxOMark官方公布了荣耀V30PRO手机的后置摄像头得分,总分为122分,目前排在总榜第二位。在后置摄像方面,荣耀V30PRO采用超感……雷军较真的卢十瓦和小金刚前世今生雷军发文表示,今天是Redmi的大日子,我们发布了小金刚系列二代:四摄小金刚RedmiNote8Pro和RedmiNote8,还有和RedmiBook14增强版笔记本和Redm……OPPO将扩大印度大诺伊达工厂产能,计划明年将产量翻倍IT之家8月11日消息据外媒报道,OPPO将扩大印度大诺伊达工厂产能,计划在2020年将产量翻倍。via91mobiles据91mobiles报道,OPPO宣布位于……三星GalaxyS22Ultra降2700元,收尾力度很大你用的上一款三星手机是什么?对于很多人来说,可能已经把目光转向了小米OV荣耀等国产品牌,完全把三星这个品牌的产品抛在了脑后。但对于一些内行人来说,现在依旧喜欢三星,为什么呢?毕……
友情链接:易事利快生活快传网聚热点七猫云快好知快百科中准网快好找文好找中准网快软网