In my project, I am calling webapi services - Get action from controller of another project. My code is:
using System.Net.Http;
using System.Configuration;
using System.Net.Http.Headers;
public ActionResult Index()
{
ViewBag.Place = new SelectList(GetPlaces(), "ID", "PlaceName");
FlightDetails flightDetails = new FlightDetails();
flightDetails.DepartureDate = System.DateTime.Now;
flightDetails.ReturnDate = System.DateTime.Now;
return View(flightDetails);
}
private List<tblPlace> GetPlaces()
{
List<tblPlace> lstPlaces = new List<tblPlace>();
using (var client = new HttpClient())
{
string apiHost = ConfigurationManager.AppSettings["ApiHost"];
string apiDirectory = "api/fromto";
List<tblPlace> result = new List<tblPlace>();
client.BaseAddress = new Uri(apiHost);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync(apiDirectory).Result;
if (response.IsSuccessStatusCode)
{
result = response.Content.ReadAsAsync<List<tblPlace>>().Result;
return result;
}
else
{
return null;
}
}
}
However, in line
ViewBag.Place = new SelectList(GetPlaces(), "ID", "PlaceName");
in Index() actionresult, I am getting error,
System.MissingMethodException: 'Method not found: 'System.Threading.Tasks.Task`1<!!0> System.Net.Http.HttpContentExtensions.ReadAsAsync(System.Net.Http.HttpContent)'.'
Web api is returning required value. The issue is with line
ReadAsAsync
. I did nothing on this part of code. It was working fine till yesterday. All of a sudden what happened, I have no clue.
What I have tried:
Found somewhere that I need to use Microsoft.Net.Http instead of System.Net.Http since this is obsolete. When I installed
Microsoft.Net.Http
from nuget package in this project, this dll was not shown in project reference.