我试图在我的 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 应用程序。
任何帮助将不胜感激。提前致谢。