在使用項目中,咱們但願去查看咱們的webapi的測試,那麼咱們是須要去有一個集成的測試的。web
1.在nutget管理包中下載swagger包。api
2.這樣會在App_start 文件夾中出現swaggerconfig.cs 和swaggerNet.cs,
這個時候就須要配置的時候了。restful
3.取消下面的註釋(swaggerconfig.cs)ide
c.IncludeXmlComments(string.Format("{0}/bin/ThinkingSpace.XML", System.AppDomain.CurrentDomain.BaseDirectory));
固然咱們爲了代碼的模塊化,能夠封裝到一個方法中:模塊化
private static string GetXmlCommentsPath() { return $@"{System.AppDomain.CurrentDomain.BaseDirectory}\bin\GetDocumentation.XML"; }
好吧,ok,咱們知道了這個配置了。測試
那麼咱們須要再bin目錄下建立一個xml,推薦是項目名.xml.ui
4.那麼接下來就是swaggerNet.cs配置.spa
using System; using System.IO; using System.Web; using System.Web.Http; using System.Web.Http.Description; using System.Web.Http.Dispatcher; using System.Web.Routing; using Swagger.Net; [assembly: WebActivator.PreApplicationStartMethod(typeof(ThinkingSpace.App_Start.SwaggerNet), "PreStart")] [assembly: WebActivator.PostApplicationStartMethod(typeof(ThinkingSpace.App_Start.SwaggerNet), "PostStart")] namespace ThinkingSpace.App_Start { public static class SwaggerNet { public static void PreStart() { RouteTable.Routes.MapHttpRoute( name: "SwaggerApi", routeTemplate: "api/docs/{controller}", defaults: new { swagger = true } ); } public static void PostStart() { var config = GlobalConfiguration.Configuration; config.Filters.Add(new SwaggerActionFilter()); try { config.Services.Replace(typeof(IDocumentationProvider), new XmlCommentDocumentationProvider(HttpContext.Current.Server.MapPath("~/bin/ThinkingSpace.XML"))); } catch (FileNotFoundException) { throw new Exception("Please enable \"XML documentation file\" in project properties with default (bin\\ThinkingSpace.XML) value or edit value in App_Start\\SwaggerNet.cs"); } } } }
統一咱們須要修改xml的位置便可。.net
咱們須要在webapi中只能存在一個get,不然會報錯,由於須要符合restful 標準。rest
一個controller中只能有一個HttpGet請求,多了就會報錯。建議減小重載方法,將其餘Get方法分開
若是在swagger.config中加上c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());則會只顯示第一個get方法
另:能夠不安裝swagger ui for .net,安了有可能會報錯