的WebAPI控制器给出空 [英] WebApi controller gives nulls
本文介绍了的WebAPI控制器给出空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧,这里是我的路线
Config.Routes.MapHttpRoute(
name: "Default",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
和我的控制器
public class GameController : ApiController
{
internal static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
[HttpGet,HttpPost]
public IEnumerable<HostedGame> List()
{
return new HostedGame[0];
}
[HttpPost]
public Guid? HostGame(HostedGameRequest request)
{
try
{
var r = this.Request.Content.ReadAsStringAsync();
var id = SasManagerHub.NextConnectionId;
//var hg = request.ToHostedGameSasRequest();
//GameManager.GetContext().SasManagerHub.Out.Client(id).StartGame(hg);
//return hg.Id;
return null;
}
catch (Exception e)
{
Log.Fatal("HostGame error",e);
return null;
}
}
}
和我的模型
public class HostedGame
{
public Guid Id { get; set; }
public string Name { get; set; }
public string HostUserName { get; set; }
public string GameName { get; set; }
public Guid GameId { get; set; }
public Version GameVersion { get; set; }
public bool HasPassword { get; set; }
}
public class HostedGameRequest : HostedGame
{
public string Password { get; set; }
}
和POST数据
{
"Id":"c883fb55-9adc-4ab8-a46c-614d2874301c",
"Name":"a",
"HostUserName":"b",
"GameName":"c",
"GameId":"c883fb55-9adc-4ab8-a46c-614d2874301c",
"GameVersion":"1.1.1.1",
"HasPassword":"true",
"Password":"asdf"
}
和网址
的http://本地主机:5879 / API /游戏/ HostGame /
好吧,由于某种原因,当我折腾的数据,这使它对在 GameController
适当的行动,但参数要求
总是空
。我已经尝试了不少变化,但似乎无法使其工作。
All right, so for some reason when I toss that data in, it makes it to the proper action in the GameController
, but the argument request
is always null
. I've tried quite a few variations, but can't seem to make it work.
任何想法我可能做错了什么?
Any ideas what I may be doing wrong here?
推荐答案
请确保您传递头内容类型:应用程序/ JSON在POST请求
Make sure you are passing the header content-type:application/json in your post requests.
这篇关于的WebAPI控制器给出空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文