.Net Core 添加Swagger步驟(Core 3.0配置步驟)

Startup.cs文件:

NuGet下載:Swashbuckle.AspNetCore

引入命名空間:

using Swashbuckle.AspNetCore.Swagger;

基本配置:

Startup.ConfigureServices方法添加:

//註冊Swagger生成器,定義一個和多個Swagger 文檔
services.AddSwaggerGen(c =>
{
     c.SwaggerDoc("v1", new Info { Title = "Vedm API V1", Version = "v1" });
});

Startup.Configure方法添加:

//啓用中間件服務生成Swagger作爲JSON終結點
app.UseSwagger();
//啓用中間件服務對swagger-ui,指定Swagger JSON終結點
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Vedm API V1");
});

啓用XML註釋:

右鍵單擊“解決方案資源管理器”中的項目,然後選擇“屬性”

查看“生成”選項卡的“輸出”部分下的“XML 文檔文件”框

Startup.ConfigureServices方法添加:

 services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new Info
                {
                    Version = "v1",
                    Title = "Vedm API V1"
                });
                // 爲 Swagger JSON and UI設置xml文檔註釋路徑
                var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//獲取應用程序所在目錄(絕對,不受工作目錄影響,建議採用此方法獲取路徑)
                var xmlPath = Path.Combine(basePath, "Lsxx.Vedm.App.xml");
                options.IncludeXmlComments(xmlPath);
            });

 

 Net Core 3.0

1、NuGet下載:Swashbuckle.AspNetCore

 

2、Startup.cs文件:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();

            //添加Swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "Api Swagger",
                    Description = "基於.NET Core 3.0 的Api Swagger"
                });
                // 加載程序集的xml描述文檔
                var baseDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
                var xmlFile = System.AppDomain.CurrentDomain.FriendlyName + ".xml";
                var xmlPath = Path.Combine(baseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);

            });
        }

        // 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();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });


            app.UseSwagger(c =>
            {
                c.RouteTemplate = "api-docs/{documentName}/swagger.json";
            });
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/api-docs/v1/swagger.json", "Api v1");
            });
        }

3、xml文檔

 

文章參考:

https://www.cnblogs.com/yilezhu/p/9241261.html

https://www.cnblogs.com/yanbigfeg/p/9232844.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章