.Net Core2.2 do use Swagger UI interface documentation

There are multiple projects under one solution. Entity such as libraries, LogicHandler library, Repository Library, Util library, Mvc project libraries.

Start

First, the project reference Swagger package of nuget

Second, configuration services

At the ConfigureServices add the following:

    //非正式环境显示Swagger页面;
            if (!_hostingEnvironment.IsProduction())
            {
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new Info { Title = "Insurance API", Version = "v1" });
                    var mvcXmlFile = $"{ Assembly.GetEntryAssembly().GetName().Name }.xml";
                    var entityXmlFile = $"Insurance.Entity.xml";
                    var mvcXmlPath = Path.Combine(AppContext.BaseDirectory, mvcXmlFile);
                    var entityXmlPath = Path.Combine(AppContext.BaseDirectory, entityXmlFile);
                    c.IncludeXmlComments(mvcXmlPath);
                    c.IncludeXmlComments(entityXmlPath);
                });
            };

Third, middleware enabled

In the Configure add the following:

 if (!_hostingEnvironment.IsProduction())
            {
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Insurance API");
                });
            }

So far Swagger basic configuration is complete, start to make the default address for the start of the project swagger interface, you can go to the project at the start launchSetting.json file modifications,

use

First, modify the project properties

After adding an annotation project, then right-click Properties generated at the start of the project, to enable xml document file, the general output path is bin \ Debug


Since Entity mvc project library references, there are many Dto Entity class library, there are many Dto comment at this time Entity project above operations, it is noted that, to make sure the output path and the xml document item mvc project Entity consistent output path so easy to find the class ConfigureServices Startup

Second, the explanatory notes on the interface Dto

If the parameter is a mandatory need to add [Required] head, if the parameter required optional digital like playing in the back type **? ** to illustrate this value can be empty.


Third, hidden Interface

Add ** [ApiExplorerSettings (IgnoreApi = true)] **, the controller may also be in the Action

Fourth, the warning is ignored Swagger comments

Error and warning may be at the start of the project attributes add code 1591;

Configuration About .Net Core3.1 can go to see Ai ternary article herein, this has to be obtained from the Department of knowledge.

Guess you like

Origin www.cnblogs.com/lucashuang/p/12566678.html