1

我正在为我的一个项目构建 ASP.NET WEB API,其中我有包含一些通用属性的通用请求消息。我想在其中构建 DelegatingHandler,我将能够从请求(无论是 JSON 请求还是 XML)中获取强类型模型作为 IRequestMessage 并验证它的一些属性。有可能吗?我怎样才能做到这一点?

public class MessageValidationHandler : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
         //what to do to get strongly typed model from request?
    }
}
4

1 回答 1

0

这是你可以做的:

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
    {
        var response = await base.SendAsync(request, cancellationToken);

        MyType type;

        if (response.TryGetContentValue(out type))
        {
            // Yay! let's do something with this!
        };

        return response;
    }
于 2013-06-20T17:27:39.000 回答