1

我试图在我的 api 中发布一些信息,这些信息是使用 WCF Web Api 编程的。在客户端中,我使用的是restsharp,它是用于restful 服务的rest 客户端。但是,当我尝试向请求中添加一些参数时,从不调用服务中的 post 方法,并且客户端中的响应对象获得 500 状态(内部服务器错误),但是当我注释行时,我' m 添加参数请求到达服务中公开的 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);
    }

这是该服务的代码:

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

我需要向我的请求添加参数,以便填充服务中的游戏对象,但我不知道如何执行此操作,如果每次我尝试添加参数时服务都会中断。

我忘了提到客户端和服务器都是 .NET MVC 3 应用程序。

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

4

3 回答 3

1

好吧,在一遍又一遍地解决这个问题之后,我终于找到了解决方案,但是,我无法解释为什么会这样。

我替换了 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);

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

于 2011-10-11T20:28:30.540 回答
1

我注意到您将 Game 作为参数并在 HttpRequestMessage 中。你不需要这样做。一旦您收到请求(即您的请求参数),您可以在 Content 属性上调用 ReadAs,您将获得 Game 实例。您两次通过 Game 的事实可能是导致问题的原因。您可以尝试删除第二个游戏参数并在响应中使用那个吗?

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

于 2011-10-12T04:59:06.793 回答
0

我不熟悉您正在调用的对象,但是 game.Name 是一个字符串吗?如果不是,这可能解释了 AddParameter 失败的原因。

于 2011-10-10T18:23:19.823 回答