0

I have a Web API project and I am trying to call some of the API(s) from an ASP.net Core application. The code is like below:

var connectionViewModel = new ConnectionViewModel()
        {
            ConnectionId = connectionId,
            MemberId = Guid.Parse(memberId),
            ReconnectOn = DateTime.Now
        };

 HttpClient client = new HttpClient();

        client.BaseAddress = new Uri("http://localhost:63892/");

        client.DefaultRequestHeaders.Accept.Clear();

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = client.PostAsJsonAsync("api/app/chat/connection/PostConnection", connectionViewModel).Result;

        response.EnsureSuccessStatusCode();

and here is the web api

[HttpPost]
    public async Task<IHttpActionResult> PostConnection(ConnectionViewModel viewModel)
    {
        var validation = await connectionService.CreateConnection(viewModel);

        if (validation.IsValid)
        {
            return Ok(resultSet.CreateResultSet(validation));
        }
        else
        {
            return BadRequest(validation.Message);
        }
    }

the problem is view Model is always null. I use the same code to call the web api from console application or another web api application and it works fine. But it does not work when I use inside asp.net core application.

Thanks

7
  • 1
    It can't be the same exact code (or POST data) if it works in other applications. Is connectionViewModel from the same assembly as what the API is using? If not, does it have the same exact properties? Commented Sep 4, 2018 at 23:42
  • Where is it null? connectionViewModel vs viewModel? Commented Sep 4, 2018 at 23:46
  • Yes, connectionViewModel is from the same assembly Commented Sep 5, 2018 at 0:00
  • viewModel in the PostConnection action is null.(input parameter) @EdSF Commented Sep 5, 2018 at 0:01
  • Have you tried explicitly serializing ConnectionViewModel before adding it as a parameter using NewtonSoft? Commented Sep 5, 2018 at 1:02

0

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.