简体   繁体   中英

Web API to API call always receives null as input parameter : .Net Core

I have two Web APIs built using.Net Core 3.1

I am trying to pass a model which has 2 different Lists as properties. Though the method in the 2nd API is called successfully from the 1st API, the properties of the model ie lists are null.

There is no issue with the API call as i could pass and receive the value when i called a method with a string as input parameter.

Here is my code

1st API method:

public async Task SomeMethod(MyModel Input, string resource)
{
   var client = new ServiceClient(BaseUrl)
        {
            Resource = resource,
        };
   client.Post(Input);
} 

resource here is the url for 2nd API Method

Model:

public class MyModel
{
  public List<Class1> List1;
  public List<Class2> List2;
}

2nd API:

    [Route("v1/myservice")]
    [ApiController]
    public class MyController : ControllerBase
    {
        private readonly IMyHandler _myHandler;
        private readonly ILogger _logger;
        private string _MyDestinationPath;
        public  MyController (IMyHandler myHandler, IConfiguration configuration, ILogger logger)
        {
            _myHandler= myHandler;
            _MyDestinationPath = configuration.GetValue<string>("DestinationPath");
            _logger = logger;
        }
        [HttpPost, Route("API2Method"), ResponseType(typeof(string))]
        public async Task API2Method([FromBody]MyModel Input)
        {
            if(!ModelState.IsValid)
            {
                return;
            }
        }
}

Can someone point out what i need to change to receive the lists i am passing to the 2nd API instead of null

We have already answer for this question, Please refer the following urls for this issue.

https://stackoverflow.com/questions/24874490/pass-multiple-complex-objects-to-a-post-put-web-api-method

For more help on parameter passing, Please refer this basic article

https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api#using-frombody

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM