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?
GetAsync(url)withapi/V1/Studentsinstead/Students[Route("api/V1/[controller]")]and i call it byGetAsync("/Students"). and it's work fine