36

我正在使用带有属性路由的 ASP.NET Web API 2。

我有以下PlayerModel

public class PlayerModel
{
    public int Id { get; set; }
    public string Key { get; set; }
    public string Name { get; set; }
    public string Password { get; set; }
    public int TeamId { get; set; }
    public PlayerStatModel Stat{ get; set; }
}


public class PlayerStatModel 
{
    public int PlayerId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Title { get; set; }
    public string EmailAddress { get; set; }
    public IEnumerable<PhoneNumberModel> PhoneNumbers { get; set; } 
    public int TeamId { get; set; }
}

public class PhoneNumberModel
{
    public string Value { get; set; }
    public string Extension { get; set; }
}

依次传递PostPlayer给玩家创建。

[HttpPost("", RouteName = "PostPlayer")]
public PlayerModel PostPlayer(PlayerModel player)
{
    var playerObject = this.GetObject(player));
    this._manager.CreatePlayer(playerObject );

    return this.GetPlayer(playerObject.Id);
}

我的集成测试通过了,我能够验证播放器在CreatePlayer被调用时确实是创建的。

如何POST在 Google Chrome 的 POSTMAN Rest Client 中建模此请求?

在此处输入图像描述

4

1 回答 1

80

好吧,请确保您指定请求标头raw并将其设置为. 然后继续并指定将匹配您的视图模型结构的 POST 请求的主体:Content-Typeapplication/json

{
    "id": 1,
    "key": "some key",
    "name": "some name of course",
    "password": "the hyper secret",
    "teamId": 256,
    "stat": {
        "playerId": 115,
        "firstName": "John",
        "lastName": "Smith",
        "title": "His Royal Majesty",
        "emailAddress": "john.smith@buckingampalace.com",
        "phoneNumbers": [
            { "value": "123", "extension": "05" },
            { "value": "456", "extension": "45" }
        ],
        "teamId": 678
    }
}

所以你的实际有效载荷在协议级别看起来像这样:

POST /NFL/Players HTTP/1.1
Host: localhost:9888
Content-Type: application/json
Content-Length: 582

{
    "id": 1,
    "key": "some key",
    "name": "some name of course",
    "password": "the hyper secret",
    "teamId": 256,
    "stat": {
        "playerId": 115,
        "firstName": "John",
        "lastName": "Smith",
        "title": "His Royal Majesty",
        "emailAddress": "john.smith@buckingampalace.com",
        "phoneNumbers": [
            { "value": "123", "extension": "05" },
            { "value": "456", "extension": "45" }
        ],
        "teamId": 678
    }
}
于 2013-09-26T20:06:43.873 回答