I am trying to connect asp.net core web api(thats already connected to my local sqlserver and working fine) to my asp.net-mvc5 web app so when the controller is called it fetches the data from database using API
Now, this is how I'm trying to connect the api in my asp.net mvc project, shown below is homeController:
namespace AptitudeTest.Controllers
{
public class HomeController : Controller
{
string Baseurl = "http://localhost:51448";
public async Task<ActionResult> Index()
{
List<Teacher> teacher = new List<Teacher>();
//Teacher t1 = new Teacher();
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Sending request to find web api REST service resource GetAllEmployees using HttpClient
HttpResponseMessage Res = await client.GetAsync("api/Admin/Get");
//Checking the response is successful or not which is sent using HttpClient
if (Res.IsSuccessStatusCode)
{
//Storing the response details recieved from web api
var EmpResponse = Res.Content.ReadAsStringAsync().Result;
//Deserializing the response recieved from web api and storing into the Employee list
teacher = JsonConvert.DeserializeObject<List<Teacher>>(EmpResponse);
}
//returning the employee list to view
return View(teacher);
}
}
I expect it to return me teachers as json objects but this is the error im getting:
The model item passed into the dictionary is of type 'System.Collections.Generic.List1[AptitudeTest.Teacher]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[AptitudeTest.Models.Teacher]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List1[AptitudeTest.Teacher]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[AptitudeTest.Models.Teacher]'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.