D
D
Devil Devil2016-08-24 16:03:25
API
Devil Devil, 2016-08-24 16:03:25

How to call the API controller through the client?

There is a project with regular controllers, there is a project with api controllers. How to call from a regular api controller through an http client (a class library that is responsible for generating requests to the ip controller), there are links to the project, but the api controller method does not work

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Devil Devil, 2016-08-24
@BarkovA

Class library, client for making queries

public CountryDTO find(int id)
        {
            try
            {
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(Base_URL);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response =  client.GetAsync("country/" + id.ToString()).Result;

                if (response.IsSuccessStatusCode)
                    return response.Content.ReadAsAsync<CountryDTO>().Result;
                return null;
            }
            catch
            {
                return null;
            }
        }

The usual controller from which we call the client
CountryClient CC = new CountryClient();
 public ActionResult Details(int id)
        {
            
            CountryDTO a = CC.find(id);
            
            return View(a);

        }

and API controller where the request should go after the client, but for some reason it doesn’t go
[ResponseType(typeof(CountryDTO))]
        public IHttpActionResult GetCountry(int id)
        {
            countryRepository.GetCoById(id);
            return Ok(countryRepository.GetCoById(id));
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question