简体   繁体   English

如何在 blazor webassembly 应用程序中获取服务器 url?

[英]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.

相关问题 如何在 blazor webassembly 应用程序(客户端)中调用 blazor 服务器应用程序(登录 webapi) - how to call blazor server app(login webapi) in blazor webassembly app(client side) Blazor Webassembly 应用程序显示来自服务器的图像 - Blazor Webassembly App display image from server 如何在客户端(blazor webassembly)调用create webapi(blazor server app) - how to call create webapi(blazor server app) in client side(blazor webassembly) 如何在发布服务器项目而不是客户端的 Azure DevOps 上为 Blazor WebAssembly 托管应用程序创建构建管道? - How to create a build pipeline for Blazor WebAssembly Hosted app on Azure DevOps that publishes the server project not the client? 如何从 blazor webassembly 中的 controller 重定向到外部 url? - How to Redirect to external url from controller in blazor webassembly? Blazor 服务器应用程序、WebAssembly-HostedByDotNetCore 和 WebAssembly 之间的一个体面的澄清? [等候接听] - A decent clarification between Blazor Server-app, WebAssembly-HostedByDotNetCore and WebAssembly? [on hold] 如何将 Blazor WebAssembly 连接到数据库 - How to connect Blazor WebAssembly to DataBase 如何访问 blazor webassembly 中的应用程序设置 - How to acess the appsettings in blazor webassembly 如何将 Blazor Serverside 与 Blazor WebAssembly 结合使用? - How to use Blazor Serverside with Blazor WebAssembly? 如何在获取参数集时调用 Blazor 服务器端应用程序中的方法 - How to call a method in Blazor Server Side App on get set of a parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM