一、新建項目
選擇空的項目webapi
查看啟動端口
創建控制器
添加方法
public class VerifController : ApiController { public IHttpActionResult GetList() { dynamic data = new { name = "李白", age = 20 }; return Json(data); } }
測試訪問(網頁get方式)
二、數據注解方式(模型驗證其實還有直接判斷方法)
1、創建數據Model
2、Model幾種注解方式
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; namespace WebApi.Models { public class Product { public int Id { get; set; } [Required] public string Name { get; set; } [Range(0, 999)] public int Price { get; set; } [Required] public string Weight { get; set; } } }
3、更改控制器代碼
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using WebApi.Models; namespace WebApi.Controllers { public class VerifController : ApiController { [HttpPost] public HttpResponseMessage GetList(Product product) { if (ModelState.IsValid) { // Do something with the product (not shown). return new HttpResponseMessage(HttpStatusCode.OK); } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } } } }
注意:必須Post,反正一般提交的數據都是Post的形式。
4、postman測試
結果如下:
4、自定義提示信息(通過過濾器方式)
1、控制器里面處理驗證
建議你使用動作過濾器進行模型驗證,所以你不需要太在意如何在你的api控制器中進行驗證:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using WebApi.Filters; using WebApi.Models; namespace WebApi.Controllers { public class VerifController : ApiController { [HttpPost] public HttpResponseMessage GetList(Product product) { if (ModelState.IsValid) { return Request.CreateResponse(HttpStatusCode.Created); } else { // the code below should probably be refactored into a GetModelErrors // method on your BaseApiController or something like that var errors = new List<string>(); foreach (var state in ModelState) { foreach (var error in state.Value.Errors) { errors.Add(error.ErrorMessage); } } return Request.CreateResponse(HttpStatusCode.Forbidden, errors); } } } }
2、過濾器創建
完整代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web; using System.Web.Http.Controllers; using System.Web.Http.Filters; namespace WebApi.Filters { public class ValidateModelAttribute:ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext.ModelState.IsValid == false) { actionContext.Response = actionContext.Request.CreateErrorResponse( HttpStatusCode.BadRequest, actionContext.ModelState); } } } }
注意:如果模型驗證失敗,則此過濾器返回包含驗證錯誤的HTTP響應。在這種情況下,不會調用控制器操作。
關於幾個引用
圖一
圖二
圖三
圖四
但是這樣還是不能完全自定義格式:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Web; using System.Web.Http.Controllers; using System.Web.Http.Filters; using Newtonsoft.Json; namespace WebApi.Filters { public class ValidateModelAttribute:ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext.ModelState.IsValid == false) { var response = actionContext.Response = actionContext.Response ?? new HttpResponseMessage(); var ModelState= actionContext.ModelState; var errors = new List<string>(); foreach (var state in ModelState) { foreach (var error in state.Value.Errors) { errors.Add(error.ErrorMessage); } } var content = new { success = false, errs = errors }; string strs = JsonConvert.SerializeObject(content); response.Content = new StringContent(strs, Encoding.UTF8, "application/json"); return; } } } }
使用方式
1、將此過濾器應用於所有Web API控制器,請在配置期間將過濾器的實例添加到HttpConfiguration.Filters集合:
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Filters.Add(new ValidateModelAttribute()); // ... } }
2、另一種選擇是將過濾器設置為各個控制器或控制器操作的屬性:
public class ProductsController : ApiController { [ValidateModel] public HttpResponseMessage Post(Product product) { // ... } }
官網案例: