C# 使用restsharp在WCF web api服务上发布http

C# 使用restsharp在WCF web api服务上发布http,c#,.net,wcf,restsharp,asp.net-web-api,C#,.net,Wcf,Restsharp,Asp.net Web Api,我试图在我的api中发布一些信息,该api是使用WCF Web api编程的。在客户端中,我使用的是restsharp,它是一个用于restful服务的rest客户端。但是,当我尝试向请求中添加一些参数时,服务中的post方法从未被调用,客户端中的响应对象获得500状态(内部服务器错误),但当我注释添加参数的行时,请求到达服务中公开的post方法 以下是来自客户端的代码: [HttpPost] public ActionResult Create(Game game) {

我试图在我的api中发布一些信息,该api是使用WCF Web api编程的。在客户端中,我使用的是restsharp,它是一个用于restful服务的rest客户端。但是,当我尝试向请求中添加一些参数时,服务中的post方法从未被调用,客户端中的响应对象获得500状态(内部服务器错误),但当我注释添加参数的行时,请求到达服务中公开的post方法

以下是来自客户端的代码:

[HttpPost]
    public ActionResult Create(Game game)
    {
        if (ModelState.IsValid)
        {
            var request = new RestRequest(Method.POST);
            var restClient = new RestClient();
            restClient.BaseUrl = "http://localhost:4778";
            request.Resource = "games";
            //request.AddParameter("Name", game.Name,ParameterType.GetOrPost); this is te line when commented     everything works fine
            RestResponse<Game> g = restClient.Execute<Game>(request);
            return RedirectToAction("Details", new {id=g.Data.Id });
        }
        return View(game);
    }
[HttpPost]
公共行动结果创建(游戏)
{
if(ModelState.IsValid)
{
var请求=新的重新请求(Method.POST);
var restClient=new restClient();
restClient.BaseUrl=”http://localhost:4778";
request.Resource=“游戏”;
//request.AddParameter(“Name”,game.Name,ParameterType.GetOrPost);这是注释一切正常时的te行
resresponse g=restClient.Execute(请求);
返回RedirectToAction(“Details”,new{id=g.Data.id});
}
返回视图(游戏);
}
以下是该服务的代码:

[WebInvoke(UriTemplate = "", Method = "POST")]
    public HttpResponseMessage<Game> Post(Game game, HttpRequestMessage<Game> request)
    {
        if (null == game)
        {
            return new HttpResponseMessage<Game>(HttpStatusCode.BadRequest);
        }
        var db = new XBoxGames();
        game = db.Games.Add(game);
        db.SaveChanges();

        HttpResponseMessage<Game> response = new HttpResponseMessage<Game>(game);
        response.StatusCode = HttpStatusCode.Created;

        var uriBuilder = new UriBuilder(request.RequestUri);
        uriBuilder.Path = string.Format("games/{0}", game.Id);
        response.Headers.Location = uriBuilder.Uri;
        return response;
    }
[WebInvoke(UriTemplate=”“,Method=“POST”)]
公共HttpResponseMessage帖子(游戏、HttpRequestMessage请求)
{
如果(空==游戏)
{
返回新的HttpResponseMessage(HttpStatusCode.BadRequest);
}
var db=新的XBoxGames();
game=db.Games.Add(游戏);
db.SaveChanges();
HttpResponseMessage response=新的HttpResponseMessage(游戏);
response.StatusCode=HttpStatusCode.Created;
var uriBuilder=新的uriBuilder(request.RequestUri);
uriBuilder.Path=string.Format(“games/{0}”,games.Id);
response.Headers.Location=uriBuilder.Uri;
返回响应;
}
我需要向我的请求中添加参数,以便填充服务中的游戏对象,但如果每次我尝试添加参数时服务中断,我不知道如何执行此操作

我忘了提到客户端和服务器都是.NETMVC3应用程序


任何帮助都将不胜感激。提前谢谢。

我不熟悉您正在调用的对象,但这是游戏。请说出一个字符串?如果不是,这可能解释AddParameter失败的原因。

在反复讨论这个问题之后,我终于找到了解决方案,但是,我无法解释为什么会发生这种情况

我替换了addBody的addParameter方法,一切都按预期进行,我可以在服务器上发布信息

问题似乎是,每当我通过addParameter方法添加参数时,该方法都会将参数作为application/x-www-form-urlencoded追加,显然WCF web api不支持这种类型的数据,这就是为什么它会向客户端返回一个内部服务器错误

相反,addBody方法使用服务器可以理解的text/xml

同样,我不知道这是否真的发生了,但似乎是这样

这就是我的客户端代码现在的样子:

[HttpPost]        
    public ActionResult Create(Game game)
    {
        if (ModelState.IsValid)
        {
            RestClient restClient = new RestClient("http://localhost:4778");
            RestRequest request = new RestRequest("games/daniel",Method.POST);
            request.AddBody(game);
            RestResponse response = restClient.Execute(request);
            if (response.StatusCode != System.Net.HttpStatusCode.InternalServerError)
            {
                return RedirectToAction("Index");
            }
        }
        return View(game);

如果您有任何反馈,或者知道发生了什么,请告诉我。

我注意到您将游戏作为一个参数,并在HttpRequestMessage中。你不需要这样做。一旦你有了请求(即你的请求参数),你就可以调用Content属性上的ReadAs,你就会得到游戏实例。你在比赛中两次传球可能是造成问题的原因。你能试着删除你的第二个游戏参数,然后在响应中使用它吗

WCF Web API不支持表单url编码。在预览5中,如果您使用的是MapServiceRoute扩展方法,它将自动连接。如果没有,则创建WebApiConfiguration对象并将其传递给ServiceHostFactory/ServiceHost