我试图在我的api中发布一些使用WCF Web Api编程的信息。在客户端中,我使用restsharp,这是restful服务的休息客户端。但是,当我尝试向请求中添加一些参数时,服务中的post方法从不会被调用,并且我的客户端响应对象获得500状态(内部服务器错误),但是当我评论我所在的行时, m添加参数,请求到达服务中暴露的post方法。使用restsharp在WCF web api服务上发布http
下面是来自客户端的代码:
[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应用程序。
任何帮助将不胜感激。提前致谢。
问题解决了。非常感谢你! – Daniel