2013-09-26 63 views
27

我正在使用帶有屬性路由的ASP.NET Web API 2。在POSTMAN Rest Client中的Web API 2 POST請求模擬

我有以下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確實創建。

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

enter image description here

回答

64

好了,請確保您指定rawContent-Type請求頭設置爲application/json。然後繼續前進,指定POST請求的主體,將符合您的視圖模型結構:

{ 
    "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": "[email protected]", 
     "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": "[email protected]", 
     "phoneNumbers": [ 
      { "value": "123", "extension": "05" }, 
      { "value": "456", "extension": "45" } 
     ], 
     "teamId": 678 
    } 
} 
+0

你們能不能進一步解釋我面臨的問題很少。 – NomanJaved

+1

在ASP.net核心中添加[FromBody]標籤前面的參數。 – Prageeth

相關問題