更新时间:2023-11-21 23:24:10
该方法在System.Net.Http.Formatting
程序集中定义,该程序集通过NuGet分发。令人困惑的是,NuGet包名称是Microsoft.AspNet.WebApi.Client
:
NuGet Gallery | Microsoft.AspNet.WebApi.Client 5.2.7 [ ^ ]
注意:async
转换您的方法不应该调用.Result
:public static async 任务< List< tblusertype>> GetAll()
{
string apiHost = ConfigurationManager.AppSettings [ ApiHost跨度>];
使用( var client = new HttpClient())
{
client.BaseAddress = new Uri(apiHost);
client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue( 应用/ JSON跨度>));
使用( var response = await client.GetAsync( api / UserType))
{
if (!response.IsSuccessStatusCode) return 空跨度>;
return await response.Content.ReadAsAsync< List< tblusertype>>();
}
}
}
另外:你正在使用HttpClient错误,这会破坏你的软件稳定性ASP.NET Monsters [ ^ 一>
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>(); //to be uncommented //lstPlaces = TwentyFour7Sewa.Utils.APIEngine<tblPlace>.GetAll("api/fromto"); 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; } } //lstPlaces = lstPlaces.OrderBy(o => o.PlaceName).ToList<tblPlace>(); //return lstPlaces; }
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.
That method is defined in theSystem.Net.Http.Formatting
assembly, which is distributed via NuGet. Confusingly, the NuGet package name isMicrosoft.AspNet.WebApi.Client
:
NuGet Gallery | Microsoft.AspNet.WebApi.Client 5.2.7[^]
NB: Theasync
conversion of your method should not call.Result
at all:public static async Task<List<tblusertype>> GetAll() { string apiHost = ConfigurationManager.AppSettings["ApiHost"]; using (var client = new HttpClient()) { client.BaseAddress = new Uri(apiHost); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); using (var response = await client.GetAsync("api/UserType")) { if (!response.IsSuccessStatusCode) return null; return await response.Content.ReadAsAsync<List<tblusertype>>(); } } }
Also: You're using HttpClient wrong and it is destabilizing your software | ASP.NET Monsters[^]