我刚刚阅读了有关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;

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

关于c# - HttpRequestMessage.CreateErrorResponse()不存在,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32706417/

10-11 02:18