To get started on
Create a model validation filter wherein ApiResp custom response class unity.
public class VldFilter:IActionFilter { /// <summary> /// 执行到action时 /// </summary> /// <param name="context"></param> public void OnActionExecuting(ActionExecutingContext context) { if (!context.ModelState.IsValid) { ApiResp result = new ApiResp(ApiRespCode.F400000) { }; StringBuilder errTxt = new StringBuilder(); foreach (var item in context.ModelState.Values) { foreach (var error in item.Errors) { errTxt.Append(error.ErrorMessage + "|"); } } if(errTxt.Length>0) { result.Message= errTxt.ToString().Substring(0, errTxt.Length - 1); } context.Result = new JsonResult(result); } } public void OnActionExecuted(ActionExecutedContext context) { } }
ApiResp probably a long way, the use of custom code in response to the enumeration, from 000,000 to 999,999, there is sufficient space to accommodate a different type of response code.
public class ApiResp { public bool Success; public string SysTime; public string Code; public string Message; public object Data; }
Set Mvc options at startup
public void ConfigureServices(IServiceCollection services) { services.AddMvc(options => { options.MaxModelValidationErrors = 5; options.Filters.Add<VldFilter>(); }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); }
SayHello interface to write a test, but not ApiResp return data type, still the default return type. Seemingly did not perform custom filter.
Api controller to the MVC controller, Cancel [ApiController] characteristics, and by the derived class to ControllerBase Controller.
Look Controller and ControllerBase difference, Controller inheritance ControllerBase and IActionFilter , ControllerBase is a base class that does not inherit any class.
Correct posture
In the startup settings ApiBehaviorOptions, enable custom model validation.
public void ConfigureServices(IServiceCollection services) { services.AddMvc(options => { options.MaxModelValidationErrors = 5; options.Filters.Add<VldFilter>(); }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.Configure<ApiBehaviorOptions>(options => { options.SuppressModelStateInvalidFilter = true; // 使用自定义模型验证 } }
Correct response class
Another correct posture
This setting, no additional VldFilter.
public void ConfigureServices (IServiceCollection Services) { services.AddMvc (Options => { options.MaxModelValidationErrors = 10 ; // options.Filters.Add <VldFilter> (); }). SetCompatibilityVersion (CompatibilityVersion.Version_2_2); // unified model verification, no VldFilter. services.Configure <ApiBehaviorOptions> (Options => {
// options.SuppressModelStateInvalidFilter = to true; // use custom model validation options.InvalidModelStateResponseFactory = (context) => { ApiResp Result= new ApiResp(ApiRespCode.F400000) { }; StringBuilder errTxt = new StringBuilder(); foreach (var item in context.ModelState.Values) { foreach (var error in item.Errors) { errTxt.Append(error.ErrorMessage + "|"); } } if (errTxt.Length > 0) { result.Message = errTxt.ToString().Substring(0, errTxt.Length - 1); } return new JsonResult(result); }; }); }