[英]How to get the server url in blazor webassembly app?
I have a .NET Web Api Server and a Blazor Webassembly Client.我有一个 .NET Web Api 服务器和一个 Z98AD8B3C99B3CA16F1F7FA84EE64C 客户端。
My problem is, that I don't know where to set the server base url on the client side.我的问题是,我不知道在客户端设置服务器基础 url 的位置。 If I would deploy it to a docker container, my guess would be, that it should come from an environment variable.
如果我将它部署到 docker 容器,我猜它应该来自环境变量。
The only solution that I found is through:我找到的唯一解决方案是:
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
At least in my local dev environment this obviously routes me always to the frontend base url.至少在我的本地开发环境中,这显然总是将我路由到前端基础 url。
So where do I need to set the backend url or how can I read out the env variable if it is deployed to a docker container so I can use it in signalR or REST-Calls?那么我需要在哪里设置后端 url 或者如果将环境变量部署到 docker 容器中,我如何读取它以便我可以在 Z2C5DC1AA9979546F12615B14902B36F 中使用它?
In Wasm you can access the 'HostEnvironment' with在 Wasm 中,您可以使用
if (builder.HostEnvironment.IsDevelopment())
{
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new
Uri(builder.HostEnvironment.BaseAddress) });
}
if (builder.HostEnvironment.IsProduction())
{
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("SomeotherAddress") });
}
await builder.Build().RunAsync();
you'll need to set the env for example: dotnet run --environment Production
doc's您需要设置环境,例如:
dotnet run --environment Production
doc's
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.