. net core2.1 webapi new swagger plug in details



Swagger is a webapi online annotation and debugging plug-in. In the past, we mainly wrote interaction documents for front-end developers or external developers by hand,

Official website address: https://swagger.io/.

But in the actual work, our document work usually lags behind the actual environment, which leads to the inconsistency between the document and the actual interface, and the front and back end developers suffer.

The appearance of swagger freed the trouble of interface document writing and improved the working efficiency of front and back end developers. The so-called “if you want to do well in your work, you must first make use of your tools”. Now let’s

Learn how to implement swagger in. Net core 2.1.

1. Nuget installation dependency package

First, nuget installs swashbuckle.aspnetcore

Open the nuget console and type the following command

Install-Package Swashbuckle.AspNetCore

2. Add swagger Middleware

public IServiceProvider ConfigureServices(IServiceCollection services)
  services.Configure<CookiePolicyOptions>(options =>
  // This lambda determines whether user consent for non-essential cookies is needed for a given request.
  options.CheckConsentNeeded = context => true;
  options.MinimumSameSitePolicy = SameSiteMode.None;
  services.AddMvc().AddJsonOptions(options =>
  //Ignore circular references
  options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  //Key without hump
  options.SerializerSettings.ContractResolver = new DefaultContractResolver();
  // Register the Swagger generator, defining 1 or more Swagger documents
  services.AddSwaggerGen(c =>
  c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
  Return registerautofac (services); // register Autofac

Reference swashbuckle.aspnetcore.swagger and enable Middleware

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  if (env.IsDevelopment())
  // Enable middleware to serve generated Swagger as a JSON endpoint.
  // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), 
  // specifying the Swagger JSON endpoint.
  app.UseSwaggerUI(c =>
  c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");

3. Configure webapi project properties

1. Double click launchsettings.json under properties to update launchurl to swagger


F5 results are as follows:

4. Add notes

As shown in the figure above, although the web API has come out, there is no comment we wrote on the action? The old driver should know that in the Framework version, we need to

Change the webapi startup attribute to add XML documents in the “project production” column, as well as. NETCORE. The following picture:


After saving, press F5 to find the production annotation. Why???? that’s because we must clearly tell swagger which path to read the webapi annotation XML file and update the configureservices under startup.

Refer to the following code:

// Register the Swagger generator, defining 1 or more Swagger documents
 services.AddSwaggerGen(options =>
  options.SwaggerDoc("v1", new Info { Title = "TestSystem", Version = "v1" });    
  //Inject webapi comment file to swagger  
  var xmlPath = Path.Combine(AppContext.BaseDirectory, "AirWebApi.xml");
  ////Class and method marks [obsolete], which can prevent the generation of [swagger document]

The code not only adds comments, but also adds configuration to prevent swagger documents from being generated, which is realized by reading the [obsolete] feature of the system.

Now, let’s look at the results~


Is it cool~~

In addition, swagger supports authorized login, which needs to be studied.


The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.

Recommended Today

Oracle (PLSQL) introduction 1

Oracle installation, user authorization, table operation, data type, DDL table, DML data. Next: Oracle introduction 2 Learning video: https://www.bilibili.com/video/BV1tJ411r7EC?p=15 Baidu cloud installation package is attached to the installation tutorial: https://blog.csdn.net/qq_ 40774525/article/details/79919057 Oracle Baidu cloud installation package: https://pan.baidu.com/s/1qVWwCUR37j2JxEecYbe5HQ Extraction code: 5abz After the Oracle service above is installed, open the client of SQL developer and the […]