我为我的模型定制了一个
public class MyBinder : IModelBinder {
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) {
// bla-bla-bla
bindingContext.ModelState.AddModelError(
bindingContext.ModelName, "Request value is invalid.");
return false;
}
}
我希望当在请求中传递无效值时,HTTP400错误请求将自动返回。然而,这种情况并不发生。如果存在任何绑定错误,应该怎样做才能使Web API返回HTTP400?
null
public sealed class BadRequestIfModelNotValidAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
/// <summary>
/// if the modelstate is not valid before invoking the action return badrequest
/// </summary>
/// <param name="actionContext"></param>
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
var modelState = actionContext.ModelState;
if (!modelState.IsValid)
actionContext.Response = generateModelStateBadRequestResponse(modelState, actionContext.Request);
base.OnActionExecuting(actionContext);//let other filters run if required
}
/// <summary>
/// if the action has done additional modelstate checks which made it invalid we are going to replace the response with a badrequest
/// </summary>
/// <param name="actionExecutedContext"></param>
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var modelState = actionExecutedContext.ActionContext.ModelState;
if (!modelState.IsValid)
actionExecutedContext.Response = generateModelStateBadRequestResponse(modelState, actionExecutedContext.Request);
base.OnActionExecuted(actionExecutedContext);
}
private HttpResponseMessage generateModelStateBadRequestResponse(IEnumerable<KeyValuePair<string, ModelState>> modelState, HttpRequestMessage request)
{
var errors = modelState
.Where(s => s.Value.Errors.Count > 0)
.Select(s => new ApiErrorMessage {
Parameter = s.Key,
Message = getErrorMessage(s.Value.Errors.First())
}) //custom class to normalize error responses from api
.ToList();
return request.CreateResponse(System.Net.HttpStatusCode.BadRequest, new ApiError
{
ExceptionType = typeof(ArgumentException).FullName,
Messages = errors
});
}
/// <summary>
/// retrieve the error message or fallback to exception if possible
/// </summary>
/// <param name="modelError"></param>
/// <returns></returns>
private static string getErrorMessage(ModelError modelError)
{
if(!string.IsNullOrWhiteSpace(modelError.ErrorMessage))
return modelError.ErrorMessage;
if(modelError.Exception != null)
return modelError.Exception.Message;
return "unspecified error";
}
}
在控制器中返回它:
if (!ModelState.IsValid)
{
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}