我刚刚阅读了web API model validation并且有这个示例,但是下面提供的示例代码无法编译,因为CreateErrorResponse()
中不存在HttpRequestMessage
。有谁知道为什么?
public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(
HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}
答案 0 :(得分:4)
CreateErrorResponse
是一种扩展方法,请添加using System.Net.Http;