別名:絲襪哥web
功能:用於生產api文檔api
Nuget搜索swagger,而後安裝Swashbucklemvc
生成api的xml文檔測試
webapi項目右鍵——屬性——生產——輸出this
漢化Swaggerspa
新建一個js,目錄/Scripts/Swagger/swagger_lang.js,屬性設置爲[嵌入的資源]code
設置Swaggerorm
修改/App_Start/SwaggerConfig.cs,注意xml地xml
using System.Web.Http; using WebActivatorEx; using cms.WebApi; using Swashbuckle.Application; [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] namespace cms.WebApi { public class SwaggerConfig { public static void Register() { var thisAssembly = typeof(SwaggerConfig).Assembly; GlobalConfiguration.Configuration .EnableSwagger(c => { c.SingleApiVersion("v1", "cms.WebApi"); c.IncludeXmlComments(string.Format("{0}/App_Data/cms.WebApi.XML", System.AppDomain.CurrentDomain.BaseDirectory));//設置xml地址 }) .EnableSwaggerUi(c => { c.DocumentTitle("My webapi"); c.InjectJavaScript(thisAssembly, "cms.WebApi.Scripts.Swagger.swagger_lang.js");//漢化js }); } } }
控制器blog
// GET api/values /// <summary> /// get請求1 /// </summary> /// <returns></returns> public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } // GET api/values/5 /// <summary> /// get請求2 /// </summary> /// <param name="id"></param> /// <returns></returns> public string Get(int id) { return "value"; }
運行測試
訪問地址:http://localhost:30022/swagger/
注意:剛開始個人webapi和mvc在一個項目中,致使漢化失敗,漢化js路徑找不到。而後把webapi單獨一個項目後正常了,坑啊!!!
end