Reference Document Source : https: //www.cnblogs.com/yilezhu/p/9241261.html
The official address https://swagger.io/
That is the code interface documentation, ie the code interface documentation
.Net core use as a demo to show the use of swagger
Create a new asp.net core web application
nuget Swashbuckle related to the introduction dll
The introduction of relevant dll file in Startup.cs
using Swashbuckle.AspNetCore.Swagger;
void ConfigureServices public (IServiceCollection Services) { services.AddMvc () SetCompatibilityVersion (CompatibilityVersion.Version_2_2);. /// Register Swagger generator, and define a plurality of documents Swagger services.AddSwaggerGen (C => { c.SwaggerDoc ( "V1 ", new new Info { the Title =" Mr. Wang's interface documentation ", Version =" v1 ", the Description =" to test the experience ", Business Card = new new Business Card () { Email =" [email protected] ", the Name =" Mr. Wang " Url = "www.baidu.com" }, License = new new License () { the Url = "www.baidu.com", the Name = "Wang" }, }); // Set the UI xml document annotation path and the JSON Swagger var Path.GetDirectoryName the basePath = (typeof (program) .Assembly.Location); // get the application directory (absolute, not the working directory, we suggest using this method to obtain the path) var XMLPath = Path.Combine (basePath, "SwaggerTest.xml"); c. IncludeXmlComments (XMLPath); }); }
void the Configure public (App IApplicationBuilder, IHostingEnvironment the env) { IF (env.IsDevelopment ()) { app.UseDeveloperExceptionPage (); } the else { . HSTS // default value of The IS On May 30 Days by You want to Change Production Scenarios for the this, See https://aka.ms/aspnetcore-hsts. app.UseHsts (); } // enable middleware services Swagger generated as JSON endpoint app.UseSwagger (); // enable middleware service swagger-ui, designated Swagger JSON endpoint app.UseSwaggerUI (C => { c.SwaggerEndpoint ( "/ swagger / v1 / swagger.json", " Wang document interface Vl"); c.RoutePrefix = string.Empty; }); app.UseHttpsRedirection (); app.UseMvc (); }
Interface part of the code:
[Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { // GET api/values /// <summary> /// 获取商户名称 /// </summary> /// <returns></returns> [HttpGet] public ActionResult<IEnumerable<string>> Get() { return new string[] { "value1", "value2" }; } // GET api/values/5 /// <summary> ///根据商户ID获取商户新鲜 /// </summary> /// <param name="id">商户ID</param> /// <returns></returns> [HttpGet("{id}")] public ActionResult<string> Get(int id) { return "value"; }
important point:
Renderings: