使用restsharp序列化对象并将其传递给WebApi而不是序列化列表

Div*_*Dan 12 c# asp.net-mvc json.net restsharp asp.net-web-api

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

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; }


}
Run Code Online (Sandbox Code Playgroud)

我有一个使用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;
    }
Run Code Online (Sandbox Code Playgroud)

在我的api中调试控制器时

 [HttpPost]
    public HttpResponseMessage Create([FromBody]StoreItemViewModel myProduct)
    {
        //check fields are valid
     .........
     }
Run Code Online (Sandbox Code Playgroud)

myProducts产品都是公共List StoreIds,它总是返回一个空Guid的单一奖励.即使我添加了2个或更多StoreIds

我认为这是因为我在我的应用程序中创建了我的帮助程序错误.

任何人都可以帮助解决这个问题引起严重的问题.

发送到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
Run Code Online (Sandbox Code Playgroud)

Pad*_*ddy 23

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;
}
Run Code Online (Sandbox Code Playgroud)

这在RestSharp 105.0.1.0中找到


Div*_*Dan 19

我设法让这个工作.我不认为它是正确的方式,但它的工作原理.

 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;
    }
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案对我有用.但是,我确实必须使用"application/json"而不是"text/json".谢谢! (5认同)

小智 14

我努力解决同样的问题并提出了一个有效的解决方案.

  1. 请务必将请求格式设置为JSON:

    request.RequestFormat = DataFormat.Json;

  2. 使用AddBody而不是AddObject:

    request.AddBody(zNewSessionUsage);

所以你的代码是这样的:

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;
}
Run Code Online (Sandbox Code Playgroud)