I am stuck at the end to find code for Web Api 2.0 (Request and Response) in Get or Post Method BODY. Can someone help me to solve this problem? I want to make body JSON loop based on Request Body in Postman.
Not yet connect to database, I just want create template code first for Postman.
- Here my code for
APIcontroller =
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ABC.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ABC.Controllers
{
public class Hitung6Controller : ApiController
{
[HttpPost]
public HttpResponseMessage Post([FromBody]Hitung7Model minta1)
{
var req1 = minta1.DistributorCode;
var hasil1 = new List<Hitung7Model>();
var hasil2 = new List<Hitung8Model>();
if (req1 != null)
{
hasil1.Add(new Hitung7Model()
{
rowNested1 = hasil2
});
hasil2.Add(new Hitung8Model()
{
});
}
return Request.CreateResponse(HttpStatusCode.OK, minta1);
}
- Here my code
Models =
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
namespace ABC.Models
{
public class Hitung7Model
{
public string DistributorCode { get; set; }
public List<Hitung8Model> rowNested1 { get; set; }
}
public class Hitung8Model
{
public string ProductCode2 { get; set; }
}
}
- I try to type Read Request many to many (
ProductCode2) like this, but the Return only 1 (ProductCode2):

In postman I type =
{
"DistributorCode" : "Abc",
"rowNested1": [
{
"ProductCode2" : "A1",
"ProductCode2" : "A2"
}
]
}
but the Return Response like this =
{
"DistributorCode": "Abc",
"rowNested1": [
{
"ProductCode2": "A2"
}
]
}
- I try Read Request body like this, but I get error in Postman and error in
APiControllerclass also get null reference.

Also I want to return infinite / unlimited loop (ProductCode2) for example I type Read Request.
In Postman Read Request:
[ProductCode2 = "A1"] until [ProductCode2 = "A100"] 100 row ProductCode2.
I need Return Response Nested loop exactly same like Read Request. in Postman Return Response:
[ProductCode2 = "A1"] loop until [ProductCode2 = "A100"] 100 row ProductCode2.
I type Postman Read Request, get error amd null reference.
{
"DistributorCode": "Abcd",
"rowNested1": [
{
"ProductCode2": "A1"
}
]
"rowNested1": [
{
"ProductCode2": "A2"
}
]
"rowNested1": [
{
"ProductCode2": "A3"
}
]
}
I need return Response structure same as like, Read Request, nested loop infinite ("ProductCode2"):

{
"DistributorCode": "Abcd",
"rowNested1": [
{
"ProductCode2": "A1"
}
]
"rowNested1": [
{
"ProductCode2": "A2"
}
]
"rowNested1": [
{
"ProductCode2": "A3"
}
]
}
Please can someone try to help me to solved my problem?
It looks like there are a couple of issues here:
Let's deal with the request body first, your models are showing you have a single
Hitung7Modelthat contains a list ofHitung8Modelobjects as a single field calledrowNested1. You'll want something like this:Now for your controller, you're going to have one instance of
Hitung7Model, and one list ofHitung8Model.Additionally, if your
Hitung8Modelis only going to contain a single string value, perhaps it's not needed at all, and you can just have a list of strings on yourHitung7Model, so the following:Which would mean you can use the following request body:
And simplify the controller accordingly, i.e. you no longer need that
Select(...)clause to get the product codes, and could just usevar productCodes = minta1.ProductCodes;.