ASP.NET Web Api-使用分块传输编码时,框架未将JSON转换为对象 [英] ASP.NET Web Api - the framework is not converting JSON to object when using Chunked Transfer Encoding

查看:141
本文介绍了ASP.NET Web Api-使用分块传输编码时,框架未将JSON转换为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android中有一个HTTP客户端,它向通过C#和ASP.NET WebApi框架实现的REST api发送HTTP PUT请求.

I have an http client in Android sending HTTP PUT requests to a REST api implemented with C# and ASP.NET WebApi framework.

只要JSON字段与C#类中的属性匹配,该框架就应该能够神奇地将JSON转换(反序列化)为模型类(普通对象).

The framework should be able to magically convert (deserialize) the JSON into a model class (plain object) as long as the JSON fields match the properties in the C# class.

当HTTP请求带有块式传输编码时出现问题,该编码使Content-Length = 0(根据 http://en.wikipedia.org/wiki/Chunked_transfer_encoding ),并且该框架无法映射Http请求消息中的JSON,因此参数为空.

The problem comes when the http requests come with Chunked Transfer Encoding that makes the Content-Length = 0 (as per http://en.wikipedia.org/wiki/Chunked_transfer_encoding) and the framework is not able to map the JSON that's within the Http request message so the parameter is null.

看这个简单的例子:

    [HttpPut]
    public HttpStatusCode SendData(int id, int count, [FromBody]MyData records, HttpRequestMessage requestMessage)
    {
        var content = requestMessage.Content;
        string jsonContent = content.ReadAsStringAsync().Result; //this gets proper JSON
        return HttpStatusCode.OK;
    }

问题在于,当客户端分块发送http请求时,记录为空.

The problem is that records is null when the client sends the http request chunked.

据我了解,Chunked Transfer编码只是http客户端或服务器在应用程序层(传输层的业务)不必担心的传输属性.但是似乎该框架无法按照我的意愿进行管理.

As I understand, the Chunked Transfer encoding is simply a transfer property that the http client or server should not have to worry about at the application layer (transport layer's business). But it seems the framework doesn't manage it as I'd like.

我可以从HttpRequestMessage手动检索JSON并将其反序列化为MyData对象,但是我将无法利用ASP.NET框架的魔力.而且您知道规则:添加的代码越多,您可能会引入的错误越多.

I could manually retrieve the JSON from the HttpRequestMessage and de-serialize it into a MyData object, but I wouldn't be able to take advantage of the ASP.NET framework's magic. And you know the rule: the more code you add the more bugs you are likely to introduce.

有没有办法处理以JSON形式发送的Http Put请求,而JSON是ASP.NET Web Api 2中编码的分块传输??

编辑:这是此示例的模型类,框架在反序列化JSON时应实例化

EDIT: This is the model class for this example that the framework should instantiate when de-serializing the JSON

public class MyData
{
    public string NamePerson {get; set;}
    public int Age {get; set;}
    public string Color {get; set;}
}

推荐答案

我最近偶然发现了同一个问题,并设法为其创建了一种解决方法.我采用了原始的JsonMediaTypeFormatter类,将其子类化,并更新了ReadFromStreamAsync/ReadFromStream-method的实现.

I recently stumbled upon the the same issue, and managed to create a workaround for it. I took the original JsonMediaTypeFormatter class, subclassed it and updated the implementation of the ReadFromStreamAsync/ReadFromStream-method.

https://gist.github.com/cobysy/578302d0f4f5b895f459

希望这会有所帮助.

这篇关于ASP.NET Web Api-使用分块传输编码时,框架未将JSON转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆