使用用第三方提供的swgger ui 幫助提高 web api 接口列表的閱讀性,并且可以在頁(yè)面中測(cè)試服務(wù)接口。 運(yùn)行程序如下: 注意:在IE中必須輸入紅色部分。 并且可以對(duì)方法進(jìn)行測(cè)試。 在開(kāi)發(fā)web api 是可以寫(xiě)清楚注釋?zhuān)⑶以谖臋n中可以全部的顯示出來(lái)。 在工程中處了安裝Swashbuckle 以外,還會(huì)用到Owin,system.web.http.owin庫(kù) 在WebApi項(xiàng)目工程中安裝:Install-Package Swashbuckle ,安裝成功能后,會(huì)在項(xiàng)目中的App_Start文件 夾中生成一個(gè)文件名為“SwaggerConfig”的文件。并修改如下: 1: using System.Web.Http; 2: using WebApi; 3: using WebActivatorEx; 4: using Swashbuckle.Application; 5: 6: [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] 7: 8: namespace WebApi 9: { 10: public class SwaggerConfig 11: { 12: public static void Register() 13: { 14: Swashbuckle.Bootstrapper.Init(GlobalConfiguration.Configuration); 15: 16: // NOTE: If you want to customize the generated swagger or UI, use SwaggerSpecConfig and/or SwaggerUiConfig here ... 17: SwaggerSpecConfig.Customize(c => 18: { 19: c.IncludeXmlComments(GetXmlCommentsPath()); 20: }); 21: } 22: 23: private static string GetXmlCommentsPath() 24: { 25: return System.String.Format(@"{0}\bin\WebApi.XML", System.AppDomain.CurrentDomain.BaseDirectory); 26: } 27: } 28: } 在工程中添加一個(gè)StartUp的文件,代碼如下: 1: 2: using Microsoft.Owin; 3: using Owin; 4: using System; 5: using System.Collections.Generic; 6: using System.Linq; 7: using System.Web; 8: using System.Web.Http; 9: 10: [assembly: OwinStartup(typeof(WebApi.Startup))] 11: namespace WebApi 12: { 13: public class Startup 14: { 15: public void Configuration(IAppBuilder app) 16: { 17: HttpConfiguration config = new HttpConfiguration(); 18: WebApiConfig.Register(config); 19: Swashbuckle.Bootstrapper.Init(config); 20: app.UseWebApi(config); 21: } 22: } 23: } 新建一個(gè)studentController: 1: namespace WebApi.Controllers 2: { 3: /// <summary> 4: /// 用戶(hù)接口 5: /// </summary> 6: public class StudentController : ApiController 7: { 8: /// <summary> 9: /// 得到所有的學(xué)生信息 10: /// </summary> 11: /// <returns></returns> 12: public IEnumerable<StudentModel> Get() 13: { 14: return new List<StudentModel>(); 15: } 16: 17: /// <summary> 18: /// 根據(jù)學(xué)生編號(hào)得到學(xué)生信息 19: /// </summary> 20: /// <param name="Id">學(xué)生編號(hào)</param> 21: /// <returns></returns> 22: public StudentModel Get(int Id) 23: { 24: return new StudentModel { }; 25: } 26: 27: /// <summary> 28: /// 添加學(xué)生 29: /// </summary> 30: /// <param name="studentModel">學(xué)生實(shí)體</param> 31: /// <remarks>添加一個(gè)新的學(xué)生</remarks> 32: /// <response code="400">Bad request </response> 33: /// <response code="500">Internal Server Error</response> 34: public void Post(StudentModel studentModel) 35: { 36: } 37: 38: 39: /// <summary> 40: /// 修改學(xué)生信息 41: /// </summary> 42: /// <param name="Id">學(xué)生編號(hào)</param> 43: /// <param name="studentModel">學(xué)生實(shí)體</param> 44: 45: [ResponseType(typeof(StudentModel))] 46: [ActionName("UpdateStudentById")] 47: public void Put(int Id, [Form]string studentModel) 48: { 49: 50: } 51: 52: /// <summary> 53: /// 刪除學(xué)生信息 54: /// </summary> 55: /// <param name="Id">學(xué)生編號(hào)</param> 56: public void Delete(int Id) 57: { 58: } 59: 60: /// <summary> 61: /// 根據(jù)學(xué)生姓名得到學(xué)生信息 62: /// </summary> 63: /// <param name="name">學(xué)生姓名</param> 64: /// <remarks>dfsafdsa</remarks> 65: /// <returns></returns> 66: //[Route(Name = "GetStudentByUserName")] 67: //[ResponseType(typeof(StudentModel))] 68: [HttpGet] 69: [ActionName("GetStudentByUserName")] 70: public IEnumerable<StudentModel> GetStudentByName(string name) 71: { 72: return new List<StudentModel>(); 73: } 74: } 75: } 設(shè)置工程屬性,在屬性的構(gòu)建中設(shè)置輸出文檔: 這里的“bin\WebApi.XML”文件名稱(chēng)和SwaggerConfig文件中的配置保持一樣。 |
|
來(lái)自: WindySky > 《 Swagger》