HttpRequestMessage.CreateErrorResponse()不存在

时间:2015-09-22 00:12:40

标签: c# asp.net-web-api

我刚刚阅读了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);
            }
        }
    }

1 个答案:

答案 0 :(得分:4)

CreateErrorResponse是一种扩展方法,请添加using System.Net.Http;

文档:https://msdn.microsoft.com/en-us/library/system.net.http.httprequestmessageextensions.createerrorresponse(v=vs.118).aspx