clarification
This article is part of the OverallAuth 2.0 series of articles, which is updated weekly with an article in the series (Complete System Development from 0 to 1).
I will try to be very detailed in this system article, so that it can be understood by novice and veteran alike.
DESCRIPTION: OverallAuth 2.0 is a simple, easy to understand and powerful Permissions + Visual Process Management system.
If you are interested, please follow me (*^▽^*).
Prerequisites for use
1. Visual Studio uses version 2022.
Building projects
OverallAuth 2.0 is still the same as OverallAuth 1.0, using the front-end and back-end separation mode, so to build the back-end, we choose the .net core web api (as shown below)
After selecting the project template, we clicked on [Next
Select .net 8.0 (the latest long term supported version), then create the project
The default project structure is shown below
Run it directly to see the default interface
Running it you can see that the system's default swagger interface is very brief and less informative for example:
1. System description, version, author, etc.
2. Information such as description and parameters of the interface.
3. Classification of interfaces, etc.
Optimizing Swagger
As said above, the default system interface documentation is very clean, next we do this in our system to make swagger look more elegant and professional.
Add a new file PlugInUnit under the project, and then create a new class SwaggerPlugInUnit under that folder.。
After building SwaggerPlugInUnit, build a class library Utility at the same level as the webapi, which is used to store the system's auxiliary tools, etc., and then build an Enum folder under the library, and create a new ModeuleGroupEnum enumeration.
As pictured:
After building the file, write an enumeration SysMenu in the ModeuleGroupEnum file and save it.
/// <summary> /// Module Grouping /// </summary> public enum ModeuleGroupEnum { SysMenu = 1, }
Then write a method in SwaggerPlugInUnit with the following code
/// <summary> /// swagger plugin /// </summary> public static class SwaggerPlugInUnit { /// <summary> /// Initializing Swagger /// </summary> /// <param name="services"></param> public static void InitSwagger(this IServiceCollection services) { //Add swagger (optinos => { typeof(ModeuleGroupEnum).GetEnumNames().ToList().ForEach(version => { (version, new OpenApiInfo() { Title = "privilege management system", Version = "V2.0", Description = "Ask for attention. Ask for a one-click trifecta.", Contact = new OpenApiContact { Name = "WeChat public number author: not just code farmer b station author: I'm not a code farmer it", Url = new Uri("") } }); }); //Reflective Acquisition Interface and Method Description var xmlFileName = $"{().GetName().Name}.xml"; ((, xmlFileName), true); }); } /// <summary> /// swagger adds routing and plumbing /// </summary> /// <param name="app"></param> public static void InitSwagger(this WebApplication app) { (); (options => { typeof(ModeuleGroupEnum).GetEnumNames().ToList().ForEach(versoin => { ($"/swagger/{versoin}/", $"Interface Classification {versoin}"); }); }); } }
Then use the custom swagger middleware in program
Do the above steps, our swagger is basically sort of built, just need to be above the controller, add routing and grouping.
/// <summary> /// system module /// </summary> [ApiController] [Route("api/[controller]/[action]")] [ApiExplorerSettings(GroupName = nameof())]
After doing all these, we have completed the optimization of swagger, only need to run the system, you can see the results!
Attention:The xml file for the interface must be generated or an error will be reported.
Well, the above is the whole process of building WebApi + optimizing Swagger, you try it!
If it helps you, please follow me (*^▽^*).
Source code address: /yangguangchenjie/overall-auth2.0-web-api
Help me Star, please.
If you are interested, please follow me (*^▽^*).
Follow me: a full-stack multi-terminal treasure blogger, regularly sharing technical articles, irregularly sharing open source projects. Follow me to bring you to know a different program world!