WebApi是微軟在VS2012 MVC4版本中綁定發行的,webapi2.0同mvc5發行的
webapi一共有以下接口返回值
1、void無返回值
2、IHttpActionResult
- Json(T content)
- Ok()、 Ok(T content)
- NotFound()
- 其他
- 自定義IHttpActionResult接口的實現
3、HttpResponseMessage
4、自定義類型
void無返回值
沒有返回值,http狀態碼204。我感覺用處不大,一個好的api應該有返回值。
public class CeshiController : ApiController { public void PostSave() { //doing } }
IHttpActionResult
IHttpActionResult是Web API 2中引入的一個接口,IHttpActionResult是HttpResponseMessage的一個工廠類。IHttpActionResult是WebAPI中推薦的標准返回值,ApiController類中也提供了不少標准的工廠函數方便我們快速構建它們,如,Json,Ok,NotFound,BadRequest等。
1、Json<T>(T content)
比較常用的方法
namespace cms.Web.API { public class CeshiController : ApiController { public studentBLL bll = new studentBLL(); public IHttpActionResult GetList() { var list = bll.FindList(); return Json(list); } public IHttpActionResult GetList2() { dynamic data = new { name = "李白", age = 20 }; return Json(data); } } }
2、Ok()、 Ok<T>(T content)
比較常用的方法
namespace cms.Web.API { public class CeshiController : ApiController { public studentBLL bll = new studentBLL(); public IHttpActionResult GetList() { var list = bll.FindList(); return Ok(list); } public IHttpActionResult GetList2() { dynamic data = new { name = "李白", age = 20 }; return Ok(data); } public IHttpActionResult PostAdd() { //表示不向客戶端返回任何信息,只告訴客戶端請求成功 return Ok(); } public IHttpActionResult GetModel(int id) { var model = bll.Find(id); return Ok(model); } } }
3、NotFound()
比較常用的方法
找不到記錄時,有時需要用到NotFound()方法
public IHttpActionResult GetModel(int id) { var model = bll.Find(id); if (model == null) { return NotFound(); } return Ok(model); }
4、其他返回值
不常用
Content<T>(HttpStatusCode statusCode, T value)
public IHttpActionResult GetCeshi() { return Content(HttpStatusCode.OK, "hello webapi"); //return Ok("hello webapi")//一樣的效果 }
BadRequest()
public IHttpActionResult GetCeshi(string name) { if (string.IsNullOrEmpty(name)) { return BadRequest(); } return Ok(); }
Redirect(string location)
public IHttpActionResult GetCeshi(string name) { return Redirect("https://www.cnblogs.com/webapi/"); }
HttpResponseMessage
這個對象也有它獨特的使用場景,需要向客戶端返回HttpResponse時就要用到這個對象。以下載為例,由於需要將下載的文件輸出到客戶端瀏覽器,Webapi的服務端需要向Web的客戶端輸出文件流,這個時候一般的IHttpActionResult對象不方便解決這個問題,於是HttpReponseMessage派上了用場。
using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Web.Http; namespace cms.Web.API { public class CeshiController : ApiController {public HttpResponseMessage GetFile() { try { var FilePath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/upload/ceshi.zip"); var stream = new FileStream(FilePath, FileMode.Open); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(stream); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Wep Api Demo ceshi.zip" }; return response; } catch { return new HttpResponseMessage(HttpStatusCode.NoContent); } } } }
自定義類型
你可以將webapi的接口和普通方法一樣,返回任意的類型,WebApi會自動序列化你自定義任何返回類型,然后將序列化的值寫到響應正文里,狀態碼統一返回200。
using System; using System.Collections.Generic; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Web.Http; using cms.BLL; using cms.Model; namespace cms.Web.API { public class CeshiController : ApiController { public studentBLL bll = new studentBLL(); public IEnumerable<student> GetList() { var list = bll.FindList(); return list; } public student GetModel(int id) { var model = bll.Find(id); if (model == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } return model; } public string GetMsg() { return "webapi"; } } }
// IHttpActionResult是WebAPI中微軟官方推薦的標准返回值,建議大家使用這個,跟着微軟走錯不了。