How to nested loop infinite in Web Api 2.0 ApiController

57 Views Asked by At

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.

  1. 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);
        }
  1. 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; }
    }
}

  1. I try to type Read Request many to many (ProductCode2) like this, but the Return only 1 (ProductCode2):

image1

In postman I type =

{
    "DistributorCode" : "Abc",
     "rowNested1": [
         {
          "ProductCode2" : "A1",
          "ProductCode2" : "A2"
         }
     ]
}

but the Return Response like this =

{
    "DistributorCode": "Abc",
    "rowNested1": [
        {
            "ProductCode2": "A2"
        }
    ]
}
  1. I try Read Request body like this, but I get error in Postman and error in APiController class also get null reference.

image2-Read Request

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"):

image3-return Response

{
    "DistributorCode": "Abcd",
    "rowNested1": [
        {
            "ProductCode2": "A1"
        }
    ]
    "rowNested1": [
        {
            "ProductCode2": "A2"
        }
    ]
     "rowNested1": [
        {
            "ProductCode2": "A3"
        }
    ]
}

Please can someone try to help me to solved my problem?

1

There are 1 best solutions below

1
David House On BEST ANSWER

It looks like there are a couple of issues here:

  1. Correctly formatting your request body.
  2. Getting the information out of the request in the controller.

Let's deal with the request body first, your models are showing you have a single Hitung7Model that contains a list of Hitung8Model objects as a single field called rowNested1. You'll want something like this:

{
    "DistributorCode" : "Abc",
    "rowNested1": [
        {
            "ProductCode2" : "A1"
        },
        {
            "ProductCode2" : "A2"
        }
    ]
}

Now for your controller, you're going to have one instance of Hitung7Model, and one list of Hitung8Model.

[HttpPost]
public HttpResponseMessage Post([FromBody]Hitung7Model minta1)
{
    var req1 = minta1.DistributorCode;
    // I've left out hasil1 as it's the same as minta1
    var hasil2 = minta1.rowNested1; // This contains the list of Hitung8Model objects, if you want the string values extracted you can use the following line
    var productCode2s = minta1.rowNested1.Select(row => row.ProductCode2).ToList();

    return Request.CreateResponse(HttpStatusCode.OK, minta1);
}

Additionally, if your Hitung8Model is 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 your Hitung7Model, so the following:

public class Hitung7Model
{
    public string DistributorCode { get; set; }
    public List<string> ProductCodes { get; set; }
}

Which would mean you can use the following request body:

{
    "DistributorCode" : "Abc",
    "ProductCodes": [
        "A1",
        "A2"
    ]
}

And simplify the controller accordingly, i.e. you no longer need that Select(...) clause to get the product codes, and could just use var productCodes = minta1.ProductCodes;.