Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# POSTMAN Rest客户端中的Web API 2 POST请求模拟_C#_Http_Rest_Post_Asp.net Web Api - Fatal编程技术网

C# POSTMAN Rest客户端中的Web API 2 POST请求模拟

C# POSTMAN Rest客户端中的Web API 2 POST请求模拟,c#,http,rest,post,asp.net-web-api,C#,Http,Rest,Post,Asp.net Web Api,我正在使用带有属性路由的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; se

我正在使用带有属性路由的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; }
}
我的集成测试通过,我能够验证调用
CreatePlayer
时是否确实创建了播放器

如何在Google Chrome中的POSTMAN Rest客户端中建模此
POST
请求


那么,请确保指定
raw
,并将
内容类型
请求头设置为
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": "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
    }
}

你能进一步解释一下吗?我遇到了一个小问题。在ASP.net core中,在参数前面添加[FromBody]标记。下面的链接解释了为什么我们应该使用[FromBody].net API
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
    }
}