ASP.NET MVC Web API 4 REST Web服务:PUT / POST - 一个对象列表

时间:2013-03-07 20:47:45

标签: rest asp.net-web-api

想知道是否可以和/或支持将整个对象放到/发布到REST Webservice,而不仅仅是一些名称/值对?

如果是这样,也可以放置/后期列出对象吗?

我认为有可能,因为GET请求能够返回一个对象列表,我想用更新的对象进行“反向”操作(而不是一次发送一个,或者更坏,通过名称/价值对的个别作品)?

我知道这是一个非常基本的问题,但我到目前为止采用的方法是尝试编写PUT并使其正常工作(如果PUT函数没有参数,则可以正常工作,例如:

public class AObjectController : ApiController
{
    public List<int[]> Put()
    {
        List<int[]> ret = new List<int[]>();

        ret.Add(new int[] {-1, 1111});
        ret.Add(new int[] {-2, 2222});

        return ret;
    }

如果我指定单个对象或对象列表,我会得到例外:

    public List<int[]> Put(AObject object)         **CASE 1**
    public List<int[]> Put(List<AObject> objects)  **CASE 2**
    {
        List<int[]> ret = new List<int[]>();

        ret.Add(new int[] { -1, 1111 });
        ret.Add(new int[] { -2, 2222 });

        return ret;
    }

案例1:公开列表(int [])Put(AObject对象)


enter image description here


案例2:公共列表(int [])Put(List(AObject)对象)


enter image description here


正在进行呼叫的客户端代码:

    public int writeAll(List<T> data)
    {
        _sendBuffer = 
            JsonConvert.SerializeObject(
                tabletData,
                new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }
            );

        byte[] b = StringHelper.GetBytes(_sendBuffer);

        string url = ContructUrlRequest(null, null);

        WebRequest request = WebRequest.Create(url);

        request.Method = "PUT";
        request.ContentType = "application/json";
        request.ContentLength = b.Length;
        request.Credentials = CredentialCache.DefaultCredentials;
        ((HttpWebRequest)request).UserAgent = "...";
        //((HttpWebRequest)request).AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(b, 0, b.Length);
            requestStream.Flush();
            requestStream.Close();
        }

        WebResponse response = request.GetResponse();

        if (response == null)
        {

            return -1;
        }

        StreamReader sr = new StreamReader(response.GetResponseStream()); ;

        _recieveBuffer = sr.ReadToEnd();

        List<int[]> _resultData = JsonConvert.DeserializeObject<List<int[]>>(
            _recieveBuffer,
            new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }
        );

        return data.Count;
    }

谢谢。

1 个答案:

答案 0 :(得分:0)

在我的客户端使用来自服务器的序列化编写代码并且它可以工作

    public int writeAll(List<AObject> aObjects)
    {
        string url = ContructUrlRequest(null, null);

        WebRequest request = WebRequest.Create(url);
        request.Method = WebRequestMethods.Http.Put;
        request.ContentType = "application/json; charset=utf-8";            
        request.Credentials = CredentialCache.DefaultCredentials;
        ((HttpWebRequest)request).UserAgent = "going insane";

        JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
        serializerSettings.TypeNameHandling = TypeNameHandling.Objects | TypeNameHandling.Arrays;
        serializerSettings.Converters.Add(new IsoDateTimeConverter());

        JsonSerializer serializer = JsonSerializer.Create(serializerSettings);

        using (Stream requestStream = request.GetRequestStream())
        {
            using (StreamWriter streamWriter = new StreamWriter(requestStream, new UTF8Encoding(false, true)))
            {
                using (JsonTextWriter jsonTextWriter = new JsonTextWriter(streamWriter))
                {
                    serializer.Serialize(jsonTextWriter, aObjects);
                }
            }
        }

        WebResponse response = request.GetResponse();

        if (response == null)
        {
            Log.Info(FIDB.TAG_WSBUFFER, "WSBuffer.writeAll: response = NULL");

            return -1;
        }

        StreamReader sr = new StreamReader(response.GetResponseStream());

        _recieveBuffer = sr.ReadToEnd();

        _resultData = JsonConvert.DeserializeObject<List<int[]>>(
            _recieveBuffer,
            new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }
        );

        return tabletData.Count;
    }