這篇文章已經過時了,新的主要配置一個就行了,請參照:http://www.cnblogs.com/alunchen/p/7397396.html
Swagger是非常流行用於編輯api給前端同事用、或者測試api的工具。
1. 首先,創建webpapi類型的項目 TestSwagger
2. 安裝swagger+swagger ui包
打開nuget界面,搜索swagger,並安裝下面兩個
安裝完成之后,可以看到這些類與文件都是安裝完成時swagger添加的
3. 打開xml文檔文件
右鍵項目屬性—>生成—>勾選XML文檔文件
4. 運行
做好上述步驟后,運行,我發現我報錯了
發生上述錯誤,請在,SwaggerNet類中,注釋類上面的兩行,就會運行成功
這時,運行成功
5. 添加注釋
我們發現,安裝完成后,寫注釋並沒有在swagger頁面上面增加,所以我們現在開開啟注釋
在SwaggerConfig類中,EnableSwagger的時候添加下面XML解析
c.IncludeXmlComments(GetXmlCommentsPath());
using System.Web.Http; using WebActivatorEx; using TestSwagger; using Swashbuckle.Application; [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] namespace TestSwagger { public class SwaggerConfig { public static void Register() { var thisAssembly = typeof(SwaggerConfig).Assembly; GlobalConfiguration.Configuration .EnableSwagger(c => { c.SingleApiVersion("v1", "TestSwagger"); //添加XML解析 c.IncludeXmlComments(GetXmlCommentsPath()); }) .EnableSwaggerUi(c => { }); } //添加XML解析 private static string GetXmlCommentsPath() { return string.Format("{0}/bin/TestSwagger.XML", System.AppDomain.CurrentDomain.BaseDirectory); } } }
注意修改相應的XML名字。