Swagger 在asp.net core中的應用2

來源:互聯網
上載者:User

標籤:asp.net core   swagger   swashbuckle.aspnetcore   

Swagger是一個把api和注釋產生一個可視(或可訪問)的輸出工具,關且還可以進行手工測試我們的api,解決了程式不想寫文檔的問題(哈哈)。

 

Swashbuckle.AspNetCore是用來解決asp.net core下的api文檔,不但能稱顯UI,還可以在UI上進行測試。

如果在asp.net core中使用swagger,首先在nuget下安裝Swashbuckle.AspNetCore,不過現在是預覽版,一定要把“包括預發行版”打上勾。

650) this.width=650;" title="swagger0001.png" src="https://s2.51cto.com/wyfs02/M02/8E/3D/wKioL1i6H5nhOmrtAAGsbe-7LLw806.png-wh_500x0-wm_3-wmp_4-s_1402315862.png" alt="wKioL1i6H5nhOmrtAAGsbe-7LLw806.png-wh_50" />

同時還要添加三個引用:

Swashbuckle.AspNetCore.SwaggerGen是產生Swagger文檔的組件

Swashbuckle.AspNetCore.Swagger:是把Swagger文檔產生Json Api的組件

Swashbuckle.AspNetCore.SwaggerUI,是把Json Api轉成頁面的組件

 

接下來設定項目屬性,產生-Output節點的XML documentation file打上勾,用來保證能把action上的注釋產生xml文檔。

同時,Controller中的action都要加http特性,這樣方例產生swagger文檔時能找到準確的api,這點很重要

接下來,就要在Starup中去調置Swagger的使用了

public void ConfigureServices(IServiceCollection services) {                services.AddMvc();     services.AddSwaggerGen(c =>     {         c.SwaggerDoc("v1", new Info         {             Title = "Swagger測試",             Version = "v1",             Description = "Swagger測試RESTful API ",             TermsOfService = "None",             Contact = new Contact             {                 Name = "桂素偉",                 Email = "[email protected]"             },         });         //設定xml注釋文檔,注意名稱一定要與項目名稱相同         var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "SwaggerDemo.xml");         c.IncludeXmlComments(filePath);         //處理複雜名稱         c.CustomSchemaIds((type) => type.FullName);     }); }       public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {     loggerFactory.AddConsole(Configuration.GetSection("Logging"));     loggerFactory.AddDebug();     app.UseMvc();     app.UseSwagger(c =>     {         //設定json路徑         c.RouteTemplate = "docs/{documentName}/swagger.json";     });     app.UseSwaggerUI(c =>     {         //訪問swagger UI的路由,如http://localhost:連接埠/docs         c.RoutePrefix = "docs";         c.SwaggerEndpoint("/docs/v1/swagger.json", "Swagger測試V1");         //更改UI樣式         c.InjectStylesheet("/swagger-ui/custom.css");         //引入UI變更js         c.InjectOnCompleteJavaScript("/swagger-ui/custom.js");     }); }

關於更多的UseSwagger參數和UseSwaggerUI參數可參考

https://github.com/domaindrivendev/Swashbuckle.AspNetCore

 

設定中的custom.css和custom.js如下

custom.css

.logo__title { font-weight:bold; font-size:0.8em;}
custom.js
var titles=document.getElementsByClassName("logo__title");titles[0].innerHTML = "Swagger測試";

關於美化UI可以運行,查看具體的html Elements,然後去寫css或js

650) this.width=650;" width="500" height="273" title="swagger0002.png" style="width:667px;height:386px;" src="https://s5.51cto.com/wyfs02/M00/8E/3D/wKioL1i6H-eSQ63pAAOSWEIb8yg179.png-wh_500x0-wm_3-wmp_4-s_3236658431.png" alt="wKioL1i6H-eSQ63pAAOSWEIb8yg179.png-wh_50" />

本文出自 “桂素偉” 部落格,請務必保留此出處http://axzxs.blog.51cto.com/730810/1903143

Swagger 在asp.net core中的應用2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.