我在服务器端有一个控制器看起来像这样:
[HttpGet]
public IActionResult Test(string returnUrl = "/")
{
return Challenge(new AuthenticationProperties() { RedirectUri = returnUrl });
}
当我按下客户端的按钮时,我打电话给:
await Http.GetAsync("/api/Login/Test");
问题是我在控制台中遇到了cors错误,没有任何反应。如果我在浏览器中手动输入url(localhost / api / Login / test),它可以正常工作。
我添加了一个看起来像这样的cors政策:
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
和
app.UseCors("CorsPolicy");
哪个没有区别。
使用此:await Http.GetJsonAsync<put here your return type>("/api/Login/Test");
这是方法签名:
public static async Task<T> GetJsonAsync<T>(this HttpClient httpClient, string requestUri)
注意:您应该使用HttpClientJsonExtensions类中定义的方法。这些方法配置为使用JavaScript Fetch Web API向Web API发出Ajax请求,更重要的是,它们知道应用程序的基本uri(<base href="/">
)
希望这可以帮助...
通过使用解决它
@inject Microsoft.AspNetCore.Components.Services.IUriHelper UriHelper
和
UriHelper.NavigateTo("/api/Login/Test", forceLoad: true);