Rest API调用,RestSharp调用ASP.NET Web API

Rest API调用,RestSharp调用ASP.NET Web API,api,rest,web,asp.net-web-api,restsharp,Api,Rest,Web,Asp.net Web Api,Restsharp,我目前正在测试用ASP.NETWebAPI编写RESTfulAPI。我在客户端上使用RestSharp模拟不同的调用 我想提交一个应用程序ID查询字符串,主体应该是“Log”类型的集合。每次,服务器接收到的主体发布的应用程序ID get总是空的 服务器上的代码: public class LogsController : ApiController { public HttpStatusCode Post(Guid ID, [FromBody] List<Log> logs

我目前正在测试用ASP.NETWebAPI编写RESTfulAPI。我在客户端上使用RestSharp模拟不同的调用

我想提交一个应用程序ID查询字符串,主体应该是“Log”类型的集合。每次,服务器接收到的主体发布的应用程序ID get总是空的

服务器上的代码:

 public class LogsController : ApiController
{
    public HttpStatusCode Post(Guid ID, [FromBody] List<Log> logs)
    {
        if (logs != null)
            return HttpStatusCode.OK;
        else
            return HttpStatusCode.PreconditionFailed;
    }
}

public class Log
{
    public Guid ErrorId { get; set; }
}
公共类日志控制器:ApiController
{
公共HttpStatusCode Post(Guid ID,[FromBody]列表日志)
{
如果(日志!=null)
返回HttpStatusCode.OK;
其他的
返回HttpStatusCode.PremissionFailed;
}
}
公共类日志
{
公共Guid错误ID{get;set;}
}
客户端上的代码:

    static void Main(string[] args)
    {
        var client = new RestClient("http://localhost:36146/api");

        var json = JsonConvert.SerializeObject(new List<Log>()
            {
                new Log { ErrorId = Guid.NewGuid()}
            });

        var request = new RestRequest("Logs", Method.POST);

        request.RequestFormat = DataFormat.Json;
        request.AddParameter("ID", Guid.NewGuid(), ParameterType.QueryString);
        request.AddHeader("Accept", "application/json");
        request.AddHeader("Content-Type", "application/json; charset=utf-8");
        request.AddBody(json);

        IRestResponse response = client.Execute(request);
        Console.WriteLine(response.Content);
        Console.Read();
    }

    public class Log
    {
        public Guid ErrorId { get; set; }
    }
static void Main(字符串[]args)
{
var client=新的RestClient(“http://localhost:36146/api");
var json=JsonConvert.SerializeObject(新列表()
{
新日志{ErrorId=Guid.NewGuid()}
});
var请求=新的重新请求(“Logs”,Method.POST);
request.RequestFormat=DataFormat.Json;
request.AddParameter(“ID”,Guid.NewGuid(),ParameterType.QueryString);
AddHeader(“接受”、“应用程序/json”);
AddHeader(“内容类型”,“应用程序/json;字符集=utf-8”);
AddBody(json);
IRestResponse response=client.Execute(请求);
Console.WriteLine(response.Content);
Console.Read();
}
公共类日志
{
公共Guid错误ID{get;set;}
}

我原以为这一切正常,但无论我现在做什么,服务器上的“logs”参数始终为空。

我想我已经找到了问题所在

RestSharp在填充请求主体时隐式使用JsonSerializer。由于我也被称为序列化程序,我认为它导致了格式问题

我已经删除了对序列化程序的调用,现在我从服务器收到了200的回复


快乐的日子。

Paul-很高兴知道您找到了解决方案,这里有一个链接:它描述了RestSharp的用法-可能会有所帮助。您可以使用此库切换到使用Json.NET-