.net core webapi搭建(1)

建立一個webapi項目

 

修改launchSettings.json

將launchSettings.json中的IIS啓動刪掉。別問我爲啥  緣由就是IISEXPRESS有時候須要我手動重啓。我嫌麻煩。html

刪除後的代碼就 變成了這個樣子web

{
  "profiles": {
    "MsgWebApi": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:62827/"
    }
  }
}

F6運行一下,修復一下DLL和看看有沒有錯誤json

爲Web Api添加Swagger幫助頁面

徹底依照官方文檔安裝swagger便可: https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs=visual-studioapi

這部分就寫了。寫一個經過nuget安裝的方法。app

Install-Package Swashbuckle.AspNetCore

以下圖visual-studio

在Startup的ConfigureServices註冊並配置Swagger, 而後在StartUp的Configure方法使用Swagger中間件:ui

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

            services.AddSwaggerGen(c =>
             {
                 c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
             });
        }

        // 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();
            }

            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });


            app.UseMvc();
        }

  好了 。第一次運行F5跑起來吧。this

http://localhost:62827/swagger/index.html  訪問地址是:url地址+swagger+index.html看不懂的話。就看不懂吧。url

運行效果以下。.net

後續

.net core qq羣:136724480

相關文章
相關標籤/搜索