0

How can use ASP.NET Core WebAPI process json request without model binding?

I have more than one hundred APIs that need to be transferred to ASP.NET Core WebAPI, but I cannot design a model for each API because the number is too much.

In addition, I also need to use Swagger, so I cannot use string parsing like IActionResult Post(string json) or IActionResult Post(JObject obj).

Controller Code(with bug):

[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
        });
    }
}

Postman:

POST /Test HTTP/1.1
Host: localhost:5000
Content-Type: application/json
Content-Length: 68

{
    "Name": "Test",
    "Person": {
        "Age": 10
    }
}

enter image description here

Current API Interface:

bool InsertPoint(PointInfo point);
bool[] InsertPoints(PointInfo[] points);
bool RemovePoint(string pointName);
bool[] RemovePoints(string[] pointNames);
string[] Search(SearchCondition condition, int count);
...

What i want to do in controller:

[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);
...

What i want to do in request:

  • RemovePoint
POST /Point/RemovePoint HTTP/1.1
Content-Type: application/json

{
    "pointName": "Test"
}
  • Search
POST /Point/Search HTTP/1.1
Content-Type: application/json

{
    "condition": {
        ...
    },
    "count": 10
}
9
  • Hi @yejinmo, why not using JObject? Commented Jul 12, 2021 at 9:10
  • This link might help you: stackoverflow.com/questions/40853188/… Commented Jul 12, 2021 at 10:17
  • You can look into dynamic as parameter. However I would be question the rationale of moving hundred apis without wanting to have dedicated models. How do you process the data in each controller without risk of misalignment, especially if there is a hundred apis? Commented Jul 12, 2021 at 10:52
  • You have a bug in your code. What do you want to fix this bug or something else? If you want to use Swagger there is no way to use any dynamic models. You have to decide what is more important. Commented Jul 12, 2021 at 12:51
  • @Rena If I use JObject, I cannot use Swagger to generate documentation. Commented Jul 13, 2021 at 3:15

1 Answer 1

2

Be sure add Newtonsoft support in your project.You could refer to the following answer:

https://stackoverflow.com/a/65101721/11398810

Then you could get data like below:

[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() });
}

With json:

{
    "Name": "Test",
    "Person": {
        "Age": 10
    }
}

Result: enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

This is a compromise solution, but it cannot: 1. Method binding based on parameter list 2. Automatically generate parameter descriptions in Swagger
It is impossible to automatically meet your new requirement because you do not want to create models for them. JObject is the fastest way to solve your original issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.