As Asked Question1 , Question2来自其他用户,但没有得到任何正确答案,所以我在这里问。
我正在使用 ASP.NET MVC 并尝试通过部分视图加载社交登录提供程序。
但我不能它给我一个错误。
这是我返回部分视图的代码:
public async Task<PartialViewResult> GetProviders()
{
string apiUrl = "mywebsite";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(apiUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("Account/externalloginpathhere");
//var result = Task.Run(async () => { await client.GetAsync("Account/externalloginpathhere"); }).Result;
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
var providers = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ViewModel>>(data);
foreach(var provider in providers)
{
if (provider.Name == "Google")
{
//code come here
}
else if (provider.Name == "Facebook")
{
//code come here
}
else if (provider.Name == "Microsoft")
{
//code come here
}
}
return PartialView("~/Views/Account/_ExternalLoginsListPartial.cshtml", providers);
}
return null;
}
}
从 Home Controller View 调用视图:
@{Html.RenderAction("GetProviders", "Account");}
这就是我所做的,请在我出错的地方纠正我!