1. 程式人生 > >.NET CORE API Swagger

.NET CORE API Swagger

false swagger exception idt netcore basepath configure bsp add

  新建一個core api 項目,使用nuget搜索Swashbuckle.AspNetCore 安裝

技術分享圖片

 修改項目生成屬性

技術分享圖片

修改啟動Startup

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            #region Swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Version = "v0.1.0",
                    Title = "Blog.Core API",
                    Description = "框架說明文檔",
                    TermsOfService = "None",
                    Contact = new Swashbuckle.AspNetCore.Swagger.Contact { Name = "core-test", Email = "[email protected]", Url = "" }
                });
                var basePath = Microsoft.DotNet.PlatformAbstractions.ApplicationEnvironment.ApplicationBasePath;
                var xmlPath = Path.Combine(basePath, "core-test.xml");//這個就是上面圖片中xml文件名
                c.IncludeXmlComments(xmlPath, true);//默認的第二個參數是false,這個是controller的註釋,記得修改
            });
            #endregion

        }

   public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                #region Swagger
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint(
"/swagger/v1/swagger.json", "ApiHelp V1"); }); #endregion } app.UseMvc(); }

現在默認啟動的路徑是http://localhost:60442/api/values,需要修改一下launchSettings.json文件

技術分享圖片

然後直接啟動項目

   技術分享圖片

.NET CORE API Swagger