6

我有一组使用 WCF Web Api 托管的服务,我需要做的是验证应用程序模型中的属性。

例如,在 MVC 3 中,我像这样装饰模型中的属性:

    [StringLength(30)]
    public string UserName { get; set; }

然后在控制器中我像这样继续验证模型是否满足验证参数:

    [HttpPost]
    ActionResult Create(Model myModel)
    { 
        if(ModelState.IsValid(){
           Post the model
        }
        else
        {
           Don't post the model
        }
    }

有没有办法在 WCF Web Api 中做类似的事情?

4

4 回答 4

6

好的,我终于设法验证了我的模型是否正常工作。我编写了一个验证处理程序和几个扩展方法。首先验证处理程序:

 public class ValidationHandler<T> : HttpOperationHandler
 {
    private readonly HttpOperationDescription _httpOperationDescription;

    public ValidationHandler(HttpOperationDescription httpOperationDescription) 
    {
        _httpOperationDescription = httpOperationDescription;
    }

    protected override IEnumerable<HttpParameter> OnGetInputParameters()
    {
        return _httpOperationDescription.InputParameters
            .Where(prm => prm.ParameterType == typeof(T));
    }

    protected override IEnumerable<HttpParameter> OnGetOutputParameters()
    {
        return _httpOperationDescription.InputParameters
            .Where(prm => prm.ParameterType == typeof(T));
    }

    protected override object[] OnHandle(object[] input)
    {
        var model = input[0];
        var validationResults = new List<ValidationResult>();
        var context = new ValidationContext(model, null, null);
        Validator.TryValidateObject(model, context, validationResults,true);
        if (validationResults.Count == 0)
        {
            return input;
        }
        else
        {
            var response = new HttpResponseMessage() 
            { 
                Content = new StringContent("Model Error"),
                StatusCode = HttpStatusCode.BadRequest
            };
            throw new HttpResponseException(response);
        }
    }
}

注意 Handler 是如何接收 T 对象的,这主要是因为我想验证 API 中的所有模型类型。因此 OnGetInputParameters 指定处理程序需要接收 T 类型对象,而 OnGetOutputParameters 指定处理程序需要返回具有相同 T 类型的对象以防验证策略满足,如果不满足,请查看 on handle 方法如何抛出让客户知道存在验证问题的异常。

现在我需要注册处理程序,为此我编写了几个扩展方法,以 Pedro Felix 的博客为例http://pfelix.wordpress.com/2011/09/24/wcf-web-apicustom-parameter-转换/(这个博客对我帮助很大,关于整个处理程序操作的事情有一些很好的解释)。所以这些是扩展方法:

public static WebApiConfiguration ModelValidationFor<T>(this WebApiConfiguration conf)
    {
        conf.AddRequestHandlers((coll, ep, desc) => 
            {
                if (desc.InputParameters.Any(p => p.ParameterType == typeof(T)))
                { 
                    coll.Add(new ValidationHandler<T>(desc));
                }
            });
        return conf;
    }

所以这个方法检查操作中是否有 T 类型参数,如果有,它会将处理程序添加到该特定操作。

这个调用另一个扩展方法 AddRequestHandler,并且该方法添加新的处理程序而不删除以前注册的处理程序(如果存在)。

public static WebApiConfiguration AddRequestHandlers(
        this WebApiConfiguration conf,
        Action<Collection<HttpOperationHandler>,ServiceEndpoint,HttpOperationDescription> requestHandlerDelegate) 
    {
        var old = conf.RequestHandlers;
        conf.RequestHandlers = old == null ? requestHandlerDelegate :
                                        (coll, ep, desc) => 
                                        {
                                            old(coll, ep, desc);
                                        };
        return conf;
    }

最后一件事是注册处理程序:

        var config = new WebApiConfiguration();
        config.ModelValidationFor<T>(); //Instead of passing a T object pass the object you want to validate
        routes.SetDefaultHttpConfiguration(config);

        routes.MapServiceRoute<YourResourceObject>("SomeRoute");

就是这样..希望它对其他人有帮助!

于 2011-11-03T16:39:05.830 回答
5

我目前正在开发一个 HttpOperationHandler ,它可以完全满足您的需求。现在还没有完成,但是这个伪代码可能会让您了解如何做到这一点。

public class ValidationHandler : HttpOperationHandler
{
    private readonly HttpOperationDescription _httpOperationDescription;
    private readonly Uri _baseAddress;

    public ValidationHandler(HttpOperationDescription httpOperationDescription, Uri baseAddress)
    {
        _httpOperationDescription = httpOperationDescription;
        _baseAddress = baseAddress;
    }

    protected override IEnumerable<HttpParameter> OnGetInputParameters()
    {
        return new[] { HttpParameter.RequestMessage };
    }

    protected override IEnumerable<HttpParameter> OnGetOutputParameters()
    {
        var types = _httpOperationDescription.InputParameters.Select(x => x.ParameterType); 

        return types.Select(type => new HttpParameter(type.Name, type));
    }

    protected override object[] OnHandle(object[] input)
    {
        var request = (HttpRequestMessage)input[0];
        var uriTemplate = _httpOperationDescription.GetUriTemplate();

        var uriTemplateMatch = uriTemplate.Match(_baseAddress, request.RequestUri);

        var validationResults = new List<ValidationResult>();

        //Bind the values from uriTemplateMatch.BoundVariables to a model

        //Do the validation with Validator.TryValidateObject and add the results to validationResults

        //Throw a exception with BadRequest http status code and add the validationResults to the message

        //Return an object array with instances of the types returned from the OnGetOutputParmeters with the bounded values
    }
}

OnGetInputParameters 值告诉 OnHandle 方法的预期输出,而 OnGetOutputParameters 告诉 OnHandle 方法的预期输出是什么(稍后将注入到服务中的方法中)。

然后,您可以使用 HttpConfiguration 将处理程序添加到路由,如下所示:

 var httpConfiguration = new HttpConfiguration
            {
                RequestHandlers = (collection, endpoint, operation) => collection.Add(new ValidationHandler(operation, endpoint.Address.Uri))
            };
 RouteTable.Routes.MapServiceRoute<MyResource>("MyResource", httpConfiguration);
于 2011-11-02T07:57:30.567 回答
3

在 MSDN 上发布了一个示例,该示例为此创建了一个应该起作用的行为。您还可以使用Validator.ValidateObject手动调用验证器(或将其包装为扩展方法)并返回验证错误,这本质上就是该行为正在执行的操作。

于 2011-11-02T00:02:24.077 回答
1

首先我应该说很棒的问题+答案丹尼尔

但是,我对其进行了更进一步的改进,对其进行了改进并添加到其中。

验证程序

我对此进行了一些细化。它现在基于泛型HttpOperationHandler,因此可以采用HttpRequestMessage. 这样做的原因是我可以返回使用正确媒体类型(从接受标头)格式化的错误消息。

public class ValidationHandler<TResource> : HttpOperationHandler<TResource, HttpRequestMessage, HttpRequestMessage>
{
    public ValidationHandler() : base("response") { }

    protected override HttpRequestMessage OnHandle(TResource model, HttpRequestMessage requestMessage)
    {
        var results = new List<ValidationResult>();
        var context = new ValidationContext(model, null, null);
        Validator.TryValidateObject(model, context, results, true);

        if (results.Count == 0)
        {
            return requestMessage;
        }

        var errorMessages = results.Select(x => x.ErrorMessage).ToArray();

        var mediaType = requestMessage.Headers.Accept.FirstOrDefault();
        var response = new RestValidationFailure(errorMessages);
        if (mediaType != null)
        {
            response.Content = new ObjectContent(typeof (string[]), errorMessages, mediaType);
        }
        throw new HttpResponseException(response);
    }
}

扩展方法

在方法desc中添加 ValidationHandler 时,您提供的 2 与不再需要的参数几乎相同ModelValidationFor

我添加了一个额外的扩展方法。这是为了确保所有“资源”类都经过验证。这主要是我懒惰和健忘。我永远忘记在某个地方的列表中添加一些类。(这就是我编写通用温莎安装程序的原因!)

public static void ValidateAllResourceTypes(this WebApiConfiguration config, string assemblyFilter = "MyCompany*.dll")
{
    var path = Path.GetDirectoryName((new Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath);
    var dc = new DirectoryCatalog(path, assemblyFilter);
    var assemblies = dc.LoadedFiles.Select(Assembly.LoadFrom).ToList();
    assemblies.ForEach(assembly =>
    {
        var resourceTypes = assembly.GetTypes()
            .Where(t => t.Namespace != null && t.Namespace.EndsWith("Resources"));

        foreach (var resourceType in resourceTypes)
        {
            var configType = typeof(Extensions);
            var mi = configType.GetMethod("ModelValidationFor");
            var mi2 = mi.MakeGenericMethod(resourceType);
            mi2.Invoke(null, new object[] { config });
        }
    });            
}

我为该类使用了System.ComponentModel.Composition.Hosting命名空间(以前称为 MEF)DirectoryCatalog。在这种情况下,我刚刚使用以“Resources”结尾的命名空间来查找我的“Resource”类。将其更改为使用自定义属性或您可能喜欢的任何其他方式来识别哪些类是您的“资源”并不需要太多工作。

休息验证失败

这是我制作的一个小助手类,用于允许验证失败响应的一致行为。

public class RestValidationFailure : HttpResponseMessage
{
    public RestValidationFailure(string[] messages)
    {
        StatusCode = HttpStatusCode.BadRequest;
        foreach (var errorMessage in messages)
        {
            Headers.Add("X-Validation-Error", errorMessage);
        }
    }
}

所以,现在我得到了所有验证错误的一个很好的列表(在我的首选媒体类型中)。

享受!:)

于 2012-01-26T22:27:29.693 回答