In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you. NET Core WebAPI how to integrate Swagger to do interface management, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
What is Swagger?
Swagger is a standardized and complete framework for generating, describing, invoking, and visualizing RESTful-style Web services.
The goal of Swagger is to define a standard and language-independent interface to REST API, which gives people and computers the ability to discover and understand services without access to source code, documents, or network traffic monitoring. When correctly defined through Swagger, the user can understand the remote service and interact with the remote service using the minimum implementation logic. Similar to the interfaces implemented for underlying programming, Swagger eliminates guesses that may occur when invoking services.
What are the advantages of Swagger?
Support API to automatically generate synchronous online documents: after using Swagger, you can generate documents directly through code, no longer need to write their own interface documents manually, it is very convenient for programmers, you can save time to write documents to learn new technologies.
Provide online testing API for Web pages: documentation is not enough, documents generated by Swagger also support online testing. The parameters and format are set, and the interface can be tested online by directly inputting the corresponding values of the parameters on the interface.
How to use Swagger in .NET Core?
(1) preparatory work
Create a .NET Core WebApi project
Create a Model class library
(2) introduce Swagger into the project
Execute the command in the WebApi project: Install-package Swashbuckle.AspNetCore
(3) add code to the ConfigureServices of StarUp.cs
# region Swagger services.AddSwaggerGen (c = > {c.SwaggerDoc ("v1", new Info {Version = "v1.1.0", Title = "Swagger WebAPI", Description = "XXX project API document", TermsOfService = "None" Contact = new Swashbuckle.AspNetCore.Swagger.Contact {Name = "XXX Project", Email = "273145719@qq.com", Url = "https://www.cnblogs.com/NBIDataVis/"}}) Var basePath = Path.GetDirectoryName (typeof (Program) .assembly.location); / / add the path to interface XML var xmlPath = Path.Combine (basePath, "TrySwaggerCore.xml"); / / if you need to display controller comments, just set the second parameter to true c.IncludeXmlComments (xmlPath, true);}); # endregion
(4) add code to the Configure of StarUp.cs
# region Swaggerapp.UseSwagger (); app.UseSwaggerUI (c = > {c.SwaggerEndpoint ("/ swagger/v1/swagger.json", "ApiDocument V1");}); # endregion
(5) enter the / swagger directory after running to check that Swagger has been enabled.
Summary of questions:
(1) if you need to set the default startup page of WebApi to Swagger, then in Properties
(2) here, you will find that the comments of the running interface are not displayed. We need to configure the annotation XML file.
Right-click on the WebAPI project-"Properties -" generate-"XML document file"
Also keep in mind that the second parameter of the code here needs to be set to True, otherwise comments at the controller level will not be displayed, only interface comments will be displayed.
(3) if you will find that there are many warnings, people with obsessive-compulsive disorder look here, we only need to filter 1591 of the warnings in the generation.
The above is how .NET Core WebAPI integrates Swagger for interface management. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.