1. Add Nuget
install-package -project XXX -version 6.4.0
2. Add static class extension method
2.1. Generate project xml: select project / right click / properties / generate / output / select xml document file
2.2.system_v1: It must be unique and does not repeat, and [("system_v1"] must be consistent with [("/swagger/system_v1/], otherwise it will be abnormal [Failed to load API definition; Fetch error: response status is 404 /swagger/system_v1/]
/// <summary> /// Swagger static class /// </summary> public static class SwaggerExtend { /// <summary> /// Add service: swagger /// </summary> /// <param name="services"></param> /// <returns></returns> public static void AddCustSwagger(this IServiceCollection services) { var version = "V1.0"; var apiName = "XXX System"; (options => { (apiDescriptions => ()); ("system_v1", new OpenApiInfo { Version = version, Title = $"{apiName} API", Description = $"{apiName} {version} Interface Service" }); // Get the directory where the application is located var basePath = (typeof(SwaggerExtend).); var xmlPath = (basePath, ""); // Swagger interface only displays method & field comments, not controller comments // The second parameter is true, which is the controller's comment //(xmlPath); (xmlPath, true); }); } /// <summary> /// Add middleware: swagger /// </summary> /// <param name="app"></param> public static void UseCustSwagger(this IApplicationBuilder app) { (); (options => { ("/swagger/system_v1/", "System API"); // The default path is: /swagger/ //Route prefix - Set to empty to jump directly to the swagger page:/ // Api test can be set to empty, it is easy to conflict with front-end routing during deployment = ; }); } }
Register the service, add middleware
public void ConfigureServices(IServiceCollection services) { (); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { (); }
This is the end of this article about NetCore configuring Swagger. For more related content on NetCore configuring Swagger, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!