[英]Serializing an object with restsharp and passing it to WebApi not serializing list
I have aa 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. 我有一个使用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 在我的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. myProducts产品都是公共List StoreIds,它总是返回一个空Guid的单一奖励。 Even if I have added 2 or more StoreIds
即使我添加了2个或更多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 发送到webapi的原始数据看起来像
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
RestSharp now has a more streamlined way to add an object to the RestRequest Body with Json Serialization: RestSharp现在有一种更简化的方法,可以使用Json序列化将对象添加到RestRequest Body:
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.AddJsonBody(objectToUpdate); // HERE
var response = client.Execute<T>(request);
return response;
}
This was found in RestSharp 105.0.1.0 这在RestSharp 105.0.1.0中找到
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;
}
I struggled with the same problem and came up a working solution. 我努力解决同样的问题并提出了一个有效的解决方案。
Be sure to set the request format to JSON: 请务必将请求格式设置为JSON:
request.RequestFormat = DataFormat.Json; request.RequestFormat = DataFormat.Json;
Use AddBody, rather than AddObject: 使用AddBody而不是AddObject:
request.AddBody(zNewSessionUsage); request.AddBody(zNewSessionUsage);
So your code would be something like this: 所以你的代码是这样的:
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.RequestFormat = DataFormat.Json;
request.AddBody(objectToUpdate);
var response = client.Execute<T>(request);
return response;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.