一、新建项目
选择空的项目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) { // ... } }
官网案例: