序列化与restsharp一个对象,并通过它的WebAPI不序列列表 [英] Serializing an object with restsharp and passing it to WebApi not serializing list

查看:254
本文介绍了序列化与restsharp一个对象,并通过它的WebAPI不序列列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像一个视图模型。

 公共类StoreItemViewModel
{
    公众的Guid ITEMID {搞定;组; }
    公开名单<&的Guid GT; StoreIds {搞定;组; }
    [需要]
    公共字符串描述{搞定;组; }
    //[需要]
    // [数据成员(IsRequired =真)]
    公众诠释ItemTypeId {搞定;组; }
}

我有使用使用RestSharp的小帮手。

 公共静态IRestResponse创建< T>(对象objectToUpdate,串apiEndPoint)其中T:新的()
    {
        VAR的客户=新RESTClient实现(CreateBaseUrl(空))
        {
            身份验证=新HttpBasicAuthenticator(用户,密码1)
        };        VAR要求=新RestRequest(apiEndPoint,Method.POST);
        //request.JsonSerializer =新JsonSerializer();
       // {RequestFormat = DataFormat.Json};
        request.AddObject(objectToUpdate);
       // clientJsonSerializer =新YourCustomSerializer();
        VAR响应= client.Execute< T>(请求);
        返回响应;
    }

在我的API中调试器

  [HttpPost]
    公众的Htt presponseMessage创建([FromBody] StoreItemViewModel myProduct的)
    {
        //检查字段有效
     .........
     }

我的产品的产品都是从公开名单StoreIds它始终将返回一个空的GUID的单奖励填充分开。即使我已经加入2个或更多StoreIds

我想这是因为我在我的应用程序中做的事情错了我创建的帮手。

任何人都可以在这方面帮助它造成一个大难题。

发送到的WebAPI的原始数据看上去像

<$p$p><$c$c>ItemId=f6dbd244-e840-47e1-9d09-53cc64cd87e6&ItemTypeId=6&Description=blabla&StoreIds=d0f36ef4-28be-4d16-a2e8-37030004174a&StoreIds=f6dbd244-e840-47e1-9d09-53cc64cd87e6&StoreId=d0f36ef4-28be-4d16-a2e8-37030004174a


解决方案

我设法得到这个工作。我不认为它正确的方式,但它的工作原理。

 公共静态IRestResponse创建&LT; T&GT;(对象objectToUpdate,串apiEndPoint)其中T:新的()
    {
        VAR的客户=新RESTClient实现(CreateBaseUrl(空))
        {
            身份验证=新HttpBasicAuthenticator(用户,密码1)
        };
        VAR JSON = JsonConvert.SerializeObject(objectToUpdate);
        VAR要求=新RestRequest(apiEndPoint,Method.POST);
        request.AddParameter(文/ JSON,JSON,ParameterType.RequestBody);
        VAR响应= client.Execute&LT; T&GT;(请求);
        返回响应;
    }

I have a a view model that looks like.

public class StoreItemViewModel
{
    public Guid ItemId { get; set; }
    public List<Guid> StoreIds { get; set; }
    [Required]
    public string Description { get; set; }
    //[Required]
    //[DataMember(IsRequired = true)]
    public int ItemTypeId { get; set; }


}

I have a small helper that using is using RestSharp.

public static IRestResponse Create<T>(object objectToUpdate, string apiEndPoint) where T : new()
    {
        var client = new RestClient(CreateBaseUrl(null))
        {
            Authenticator = new HttpBasicAuthenticator("user", "Password1")
        };

        var request = new RestRequest(apiEndPoint, Method.POST);
        //request.JsonSerializer = new JsonSerializer();
       // {RequestFormat = DataFormat.Json};
        request.AddObject(objectToUpdate);
       // clientJsonSerializer = new YourCustomSerializer();
        var response = client.Execute<T>(request);
        return response;
    }

When debugging the controller within my api

 [HttpPost]
    public HttpResponseMessage Create([FromBody]StoreItemViewModel myProduct)
    {
        //check fields are valid
     .........
     }

myProducts products are all populated apart from the public List StoreIds it always is returning a single reward with an empty Guid. Even if I have added 2 or more StoreIds

I assume this is because I am doing something wrong with my Create helper within my application.

Can anyone help with this its causing a major headache.

The raw data sent to the webapi is looking like

ItemId=f6dbd244-e840-47e1-9d09-53cc64cd87e6&ItemTypeId=6&Description=blabla&StoreIds=d0f36ef4-28be-4d16-a2e8-37030004174a&StoreIds=f6dbd244-e840-47e1-9d09-53cc64cd87e6&StoreId=d0f36ef4-28be-4d16-a2e8-37030004174a

解决方案

I managed to get this working. I don't think its the correct way but it works.

 public static IRestResponse Create<T>(object objectToUpdate, string apiEndPoint) where T : new()
    {
        var client = new RestClient(CreateBaseUrl(null))
        {
            Authenticator = new HttpBasicAuthenticator("user", "Password1")
        };
        var json = JsonConvert.SerializeObject(objectToUpdate);
        var request = new RestRequest(apiEndPoint, Method.POST);
        request.AddParameter("text/json", json, ParameterType.RequestBody);
        var response = client.Execute<T>(request);
        return response;
    }

这篇关于序列化与restsharp一个对象,并通过它的WebAPI不序列列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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