Json ASP.NET 核心 WebAPI 中没有模型绑定的请求
Json Request Without Model Binding In ASP.NET Core WebAPI
如何在没有模型绑定的情况下使用 ASP.NET Core WebAPI 处理 json 请求?
我有一百多个API需要转移到ASP.NET Core WebAPI,但我无法为每个API设计一个模型,因为数量太多了。
此外,我还需要使用Swagger,所以不能使用IActionResult Post(string json)
或IActionResult Post(JObject obj)
.
这样的字符串解析
控制器代码(有错误):
[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
public class Person { public int Age { get; set; } }
[HttpPost]
public IActionResult Post([FromBody] string name, Person person)
{
return Ok(new
{
Name = name,
person.Age
});
}
}
邮递员:
POST /Test HTTP/1.1
Host: localhost:5000
Content-Type: application/json
Content-Length: 68
{
"Name": "Test",
"Person": {
"Age": 10
}
}
当前API接口:
bool InsertPoint(PointInfo point);
bool[] InsertPoints(PointInfo[] points);
bool RemovePoint(string pointName);
bool[] RemovePoints(string[] pointNames);
string[] Search(SearchCondition condition, int count);
...
我想在控制器中做什么:
[HttpPost]
[SomeJsonBodyAttribute]
public IActionResult InsertPoint(PointInfo point);
[HttpPost]
[SomeJsonBodyAttribute]
public IActionResult InsertPoints(PointInfo[] points);
[HttpPost]
[SomeJsonBodyAttribute]
public IActionResult RemovePoint(string pointName);
[HttpPost]
[SomeJsonBodyAttribute]
public IActionResult RemovePoints(string[] pointNames);
[HttpPost]
[SomeJsonBodyAttribute]
public IActionResult Search(SearchCondition condition, int count);
...
我想在请求中做什么:
- 移除点
POST /Point/RemovePoint HTTP/1.1
Content-Type: application/json
{
"pointName": "Test"
}
- 搜索
POST /Point/Search HTTP/1.1
Content-Type: application/json
{
"condition": {
...
},
"count": 10
}
请务必在您的 project.You 中添加 Newtonsoft 支持 可参考以下答案:
那么你可以得到如下数据:
[HttpPost]
public IActionResult Post([FromBody] JObject obj)
{
//for list model:
//obj["Person"][0]["Age"].ToString()
return Ok(new { Name = obj["Name"].ToString(), Age = obj["Person"]["Age"].ToString() });
}
与json:
{
"Name": "Test",
"Person": {
"Age": 10
}
}
结果:
如何在没有模型绑定的情况下使用 ASP.NET Core WebAPI 处理 json 请求?
我有一百多个API需要转移到ASP.NET Core WebAPI,但我无法为每个API设计一个模型,因为数量太多了。
此外,我还需要使用Swagger,所以不能使用IActionResult Post(string json)
或IActionResult Post(JObject obj)
.
控制器代码(有错误):
[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
public class Person { public int Age { get; set; } }
[HttpPost]
public IActionResult Post([FromBody] string name, Person person)
{
return Ok(new
{
Name = name,
person.Age
});
}
}
邮递员:
POST /Test HTTP/1.1
Host: localhost:5000
Content-Type: application/json
Content-Length: 68
{
"Name": "Test",
"Person": {
"Age": 10
}
}
当前API接口:
bool InsertPoint(PointInfo point);
bool[] InsertPoints(PointInfo[] points);
bool RemovePoint(string pointName);
bool[] RemovePoints(string[] pointNames);
string[] Search(SearchCondition condition, int count);
...
我想在控制器中做什么:
[HttpPost]
[SomeJsonBodyAttribute]
public IActionResult InsertPoint(PointInfo point);
[HttpPost]
[SomeJsonBodyAttribute]
public IActionResult InsertPoints(PointInfo[] points);
[HttpPost]
[SomeJsonBodyAttribute]
public IActionResult RemovePoint(string pointName);
[HttpPost]
[SomeJsonBodyAttribute]
public IActionResult RemovePoints(string[] pointNames);
[HttpPost]
[SomeJsonBodyAttribute]
public IActionResult Search(SearchCondition condition, int count);
...
我想在请求中做什么:
- 移除点
POST /Point/RemovePoint HTTP/1.1
Content-Type: application/json
{
"pointName": "Test"
}
- 搜索
POST /Point/Search HTTP/1.1
Content-Type: application/json
{
"condition": {
...
},
"count": 10
}
请务必在您的 project.You 中添加 Newtonsoft 支持 可参考以下答案:
那么你可以得到如下数据:
[HttpPost]
public IActionResult Post([FromBody] JObject obj)
{
//for list model:
//obj["Person"][0]["Age"].ToString()
return Ok(new { Name = obj["Name"].ToString(), Age = obj["Person"]["Age"].ToString() });
}
与json:
{
"Name": "Test",
"Person": {
"Age": 10
}
}
结果: