我刚刚读到关于 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);
}
}
}
请您参考如下方法:
CreateErrorResponse是一个扩展方法,添加using System.Net.Http;
