1

I am trying to call my webapi locally. This is my postman url and it work great. http://localhost:8080/api/V1/Students

When calling from MVC application I get an exception 404 not found.

This is my student controller

            var url = "/Students";
            string test = ApiHelper.ApiClient.BaseAddress + url;
            using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url))
            {
                if (response.IsSuccessStatusCode)
                {
                    listStudent = await response.Content.ReadAsAsync<List<StudentModel>>();
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                }
            }

Notice: test actually return the url "http://localhost:8080/api/V1/Students". Witch his good.

And this is my ApiHelper code.

    public class ApiHelper
    {
        public static HttpClient ApiClient { get; set; }

        public static void  InitializeClient()
        {
            string ApiBaseUrl = ConfigurationManager.AppSettings["ApiUrl"];
            ApiClient = new HttpClient();
            ApiClient.BaseAddress = new Uri(ApiBaseUrl);
            ApiClient.DefaultRequestHeaders.Accept.Clear();
            ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }
    }

When I debug it I found that on my response the Request URI

RequestUri  {http://localhost:8080/Student}

This is where my api location is called

  <appSettings>
    <add key="ApiUrl" value="http://localhost:8080/api/V1" />
  </appSettings>

What I am doing wrong in trying to call the local api?

6
  • 1
    try to call GetAsync(url) with api/V1/Students instead /Students Commented Jun 12, 2020 at 21:01
  • This did the work, however why would i put api/v1 in all my web api call this sound overkill. Commented Jun 12, 2020 at 21:20
  • i don't know how you defined the route for the API, but i tried to add just [Route("api/V1/[controller]")] and i call it by GetAsync("/Students"). and it's work fine Commented Jun 12, 2020 at 21:41
  • No, What I meant to say is if I do GetASync(api/V1/Students) it works, but declaring [route(...)] for every controller/method seems tedious to write it on every single method/controller. what if i want to change from v1 to v2, I then need to change all my method from route(api/v1... to route(api/v2...) this is kinda why i created the apihelper so i could have my based element in one central location. Your solution but on each controller. I may have thousand controller in the future. But yes your solution does work. Commented Jun 12, 2020 at 23:23
  • In this case, change url by test for GetAsync method. And will work to Commented Jun 12, 2020 at 23:32

1 Answer 1

1

api/v1 is a route prefix. Decorate your BaseAddress controller with this route prefix and tray again. Like so:

[RoutePrefix("api/V1")]  
    public class ProductController : Controller  
    { 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.