我試圖在我的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