C# POSTMAN Rest客户端中的Web API 2 POST请求模拟
我正在使用带有属性路由的ASP.NET Web API 2 我有以下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
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
}
}