我正在为我的一个项目构建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?
}
}
答案 0 :(得分: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;
}