1. 程式人生 > >ASP.Net MVCWebApi下整合Swagger UI(.NetCore 和.NetFramework框架)

ASP.Net MVCWebApi下整合Swagger UI(.NetCore 和.NetFramework框架)

.NetFramework框架

1. 安裝Swashbuckle v5.6.0 Nuget包(目前最新版)

在這裡插入圖片描述

2. 解決方案>屬性>生成

在這裡插入圖片描述

3. 新增配置

引入Swashbuckle包,App_Start資料夾會自動新增 SwaggerConfig.cs 類,內部方式預設被註釋掉了,取消 c.IncludeXmlComments(GetXmlCommentsPath());該句的註釋,並在下方新增方法:

        private static string GetXmlCommentsPath()
        {
            return
string.Format(@"{0}\bin\TransactionSearch.xml", AppDomain.CurrentDomain.BaseDirectory); }

在這裡插入圖片描述
在這裡插入圖片描述

4. 控制器內編寫API介面

在這裡插入圖片描述

5. 瀏覽器執行

輸入地址: http://xxxx/swagger
在這裡插入圖片描述

.NetCore框架

1. 引入 Swashbuckle 最新版本

在這裡插入圖片描述

2. 編寫文件過濾器

繼承 IDocumentFilter

public class TagDescriptionsDocumentFilter : IDocumentFilter
{ /// <summary> /// Apply /// </summary> /// <param name="swaggerDoc"></param> /// <param name="context"></param> public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context) { swaggerDoc.Tags =
new[] { new Tag{ Name = "PersonTransaction", Description = "個人交易" }, new Tag{ Name = "GroupTransaction", Description = "機構交易" } }; } }

3. Startup.cs類中註冊

 public class Startup
    {
        private readonly IHostingEnvironment _hostingEnv;

        public Startup(IHostingEnvironment env, IConfiguration configuration)
        {
            _hostingEnv = env;
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Title = "xxxx介面",
                    Version = "v1",
                    Description = ""
                });

                // 註釋
                c.IncludeXmlComments($"{AppContext.BaseDirectory}{Path.DirectorySeparatorChar}{_hostingEnv.ApplicationName}.xml");

                // Tags描述
                c.DocumentFilter<TagDescriptionsDocumentFilter>();
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/api/Home/Error");
            }

            app.UseSwagger(c =>
            {
                c.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.Host = httpReq.Host.Value);
            });

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "xxxx介面");
            });

            app.UseMvc();
        }
    }

4. 控制器下編寫API介面

在這裡插入圖片描述

5. 瀏覽器執行

執行地址: https://xxxx/swagger
在這裡插入圖片描述