没有详细说明我为什么要这样做,这就是我想做的事。
使用jquery发布Json数据,如下所示:
jq.ajax({
url: 'api/values',
type: 'POST',
dataType: 'json',
data: { a: 'hello', b: 'world' }
});
在WebAPI DelegatingHandler中,我想将该内容解析回Json:
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var data = request.Content.ReadAsStringAsync().Result;
//data is url encoded, I want it as json...
//???
//...I'm going to do something with the json string here
var response = await base.SendAsync(request, cancellationToken);
return response;
}
Json.NET将在它到达处理它的WebAPI方法时执行此操作,所以显然它是可行的,我只是不确定如何。
请注意,我不需要强类型或类似的东西 - 普通的Json字符串会这样做。