0

How can i get returned Value from web.api to mvc controller with json?

Code from mvc controller:

try
{
    HttpClient cl = new HttpClient();
    cl.BaseAddress = new Uri("http://localhost:35936");
    cl.DefaultRequestHeaders.Add("ApiUser", "User");
    cl.DefaultRequestHeaders.Add("Password", "Password");
    cl.DefaultRequestHeaders.Add("MyVar", "1234");
    cl.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));               
    string json = JsonConvert.SerializeObject(testedVar);
    var sucSender = await cl.PostAsJsonAsync("api/sender/successtested", json);
    if (sucSender.IsSuccessStatusCode)
    {
       var newvar = JsonConvert.DeserializeObject<Tested>(sucSender.ToString());                 
    } 
}

Code from Web.Api:

string forReturn = "222444";
Tested forRetVar = new Tested
{
    test = forReturn
};
var test123 = JsonConvert.SerializeObject(forRetVar);
return  JsonConvert.SerializeObject(forRetVar);          
Jim Hewitt
  • 1,726
  • 4
  • 24
  • 26

1 Answers1

0

You have to modify how you are posting and reading the response.

Modify the post call from

var sucSender = await cl.PostAsJsonAsync("api/sender/successtested", json);

to

var sucSender = await cl.PostAsJsonAsync("api/sender/successtested", json).ConfigureAwait(false);

And modify the response read call from doing this

var newvar = JsonConvert.DeserializeObject<Tested>(sucSender.ToString());

To

var newvar = await sucSender.Content.ReadAsAsync<Tested>();

here is full code based on my own test sample. I have a test2 api controller hosted at http://localhost/MyTestTwo which internally calls another api hosted at http://localhost/MyTestApi using HttpClient. To keep it simple, I have kept Test2Controller Get method sync instead of converting it into async await pattern. Both webapi applications are configured in IIS with Anonymous authentication.

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web.Http;

namespace MyTestTwo.Controllers
{
    public class Test2Controller : ApiController
    {
        public IHttpActionResult Get()
        {
            var employeesTask = GetEmp();
            employeesTask.Wait();

            var employees = employeesTask.Result;

            return Ok(employees);
        }

        private async Task<IEnumerable<string>> GetEmp()
        {
            IEnumerable<string> response = new List<string>();
            var cl = new HttpClient();
            cl.BaseAddress = new Uri("http://localhost/MyTestApp/");
            cl.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var sucSender = await cl.PostAsJsonAsync("api/MyTest", "test").ConfigureAwait(false);
            if (sucSender.IsSuccessStatusCode)
            {
                response = await sucSender.Content.ReadAsAsync<IEnumerable<string>>();
            }

            return response;
        }
    }
}

Please refer to this post Get response from PostAsJsonAsync and HttpClient.GetAsync(...) never returns when using await/async for more details.

Community
  • 1
  • 1
Vinod
  • 1,882
  • 2
  • 17
  • 27