net core 3.1 swagger文檔添加 不用xml配置


使用特性來描述接口而不是xml文件,使用特性可自定義接口在swaggerUI上的描述

安裝nuget包:Swashbuckle.AspNetCore.SwaggerUISwashbuckle.AspNetCore.Annotations,配置swagger:

 1  public void ConfigureServices(IServiceCollection services)
 2         {
 3             services.Configure<IConfiguration>(Configuration); 4           
 5 
 6             #region 添加Swagger
 7             services.AddSwaggerGen(opt=> {
 8                 opt.SwaggerDoc(swaggerDocName, new OpenApiInfo() { Version="v1",Title="watch api",Description="watch"});
 9                 //使用annotation來描述接口  不依賴xml文件
10                 opt.EnableAnnotations();
11 
12                 // 下面兩句,將swagger文檔中controller名使用GroupName替換
13                 // 在Swagger中,一個Tag可以看作是一個API分組
14                 opt.DocInclusionPredicate((_, apiDescription) => string.IsNullOrWhiteSpace(apiDescription.GroupName) == false);
15                 opt.SwaggerGeneratorOptions.TagsSelector = (apiDescription) => new[] { apiDescription.GroupName };
16 
17             });
18 
19             #endregion
20 
21             services.AddControllers();
22 
23 
24 
25 
26 
27         }

使用上面注入好的swagger

  // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {




            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            #region Swagger

            app.UseSwagger(opt =>
            {
                // 相對路徑加載swagger文檔
                //opt.RouteTemplate = "swagger/{documentName}";
            })
             .UseSwaggerUI(opt =>
             {
                 opt.SwaggerEndpoint($"{swaggerDocName}/swagger.json", "watch API文檔");
             });
            #endregion
            app.UseHttpsRedirection();

            app.UseRouting();

           
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

Controller和Action上使用特性:ApiExplorerSettingsSwaggerOperation

namespace WatchApi.Controllers
{
    [ApiExplorerSettings(GroupName = "品牌")]
    [Route("[controller]/[action]")]
    [ApiController]
    public class pinpaiController : ControllerBase
    {
        private readonly IMapper _mapper;
        private readonly IpinpaiService _pinpaiserver;
        public pinpaiController(IpinpaiService pinpaiserver, IMapper mapper)
        {
            _pinpaiserver = pinpaiserver;
            _mapper = mapper;
        }
        [SwaggerOperation(Summary = "獲取列表")]
        [HttpGet]
        public ResponseModel<IEnumerable<pinpai>> Get()
        {
            var resModel = _pinpaiserver.GetListpinpai();
            return ResponseModel.Succeed<IEnumerable<pinpai>>(resModel);
        }
        [SwaggerOperation(Summary = "添加商品")]
        [HttpPost,Authorize]
        public ResponseModel Add(pinpai pinpaiobj)
        {
            //pinpaiRequest obj
            //var pinpaiobj1 = _mapper.Map<pinpai>(pinpaiobj);
            var result = _pinpaiserver.Addpinpai(pinpaiobj);
            if (result)
            {
                return ResponseModel.Succeed();
            }
            else
            {
                return ResponseModel.Failed();
            }

        }

        [HttpPost]
        public ResponseModel<string> Add1([FromForm] string name)
        {
            return ResponseModel.Succeed(name);
        }

    }
}

訪問https://localhost:5001/swagger/index.html   換成自己的調試鏈接

效果圖如下

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM