0

好的,这是我的路线

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

和帖子数据

{
    "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://localhost:5879/api/game/HostGame/

好吧,所以由于某种原因,当我把数据扔进去时,它GameControllerrequestnull. 我已经尝试了很多变化,但似乎无法使其工作。

有什么想法我可能在这里做错了吗?

4

1 回答 1

1

确保您在发布请求中传递标头 content-type:application/json。

于 2013-02-22T03:59:06.363 回答