ASP.NET Core 2.0;发送GET请求(HttpClient)后,我的程序跳过代码中的返回块,并将当前操作重定向到Error页面,而不会捕获异常。您可以在线观看有问题的视频或下载here 对视频中的音乐感到抱歉!
public IActionResult VKAuth()
{
bool ok = Request.Query.TryGetValue("code", out var value);
if (!ok)
return RedirectToActionPermanent("Error");
else // ok == true, go here
{
try
{
GetAccessToken(value).Wait(); // it's ok
return RedirectToActionPermanent("Deleting"); // skips this return block
}
catch
{
return RedirectToActionPermanent("Error");
}
}
} // then program stops here and then next step is error action of controller
private async Task GetAccessToken(string value)
{
string Json;
using (var handler = new HttpClientHandler() { ClientCertificateOptions = ClientCertificateOption.Manual, SslProtocols = SslProtocols.Tls|SslProtocols.Tls11|SslProtocols.Tls12 })
using (HttpClient Client = new HttpClient(handler) { BaseAddress = new System.Uri("https://oauth.vk.com/") })
Json = await Client.GetStringAsync($"access_token?client_id={ClientId}&client_secret={ClientSecret}&redirect_uri={Domain}/{AuthRoute}&code=" + value);
JsonTokenResponseModel TokenModel = JsonConvert.DeserializeObject<JsonTokenResponseModel>(Json);
AccessToken = new UserAccessToken(TokenModel.access_token, TokenModel.expires_in);
}
更新
public IActionResult Deleting()
{
VKChange();
return View();
}
private async void VKChange()
{
try
{
await ClearPage();
Response.Redirect(Url.Action("Success", "SocialSuicide"), true);
}
catch
{
Response.Redirect(Url.Action("Error", "SocialSuicide"), true);
}
}
更新2:
public IActionResult Deleting()
{
Request.Cookies.TryGetValue("code", out string code);
ViewData["Token"] = "https://oauth.vk.com/" + $"access_token?client_id={ClientId}&client_secret={ClientSecret}&redirect_uri={Domain}/{AuthRoute}&code=" + code;
ViewData["WallScript"] = Method($"execute?code={ExecuteWallScript}");
ViewData["ApiVersion"] = ApiVersion;
return View();
}
public IActionResult VKAuth()
{
bool ok = Request.Query.TryGetValue("code", out var value);
if (!ok)
return RedirectToActionPermanent("Error");
else
{
Response.Cookies.Append("code", value);
return RedirectToActionPermanent("Deleting", "SocialSuicide");
}
}
我还有问题,我删除了所有异步方法。