How can I manage access variables which differ among environments in client side blazor? Normally since I use Azure to publish applications, I'd use the appsettings.json
file for local app settings and then set up entries in the Azure Application Settings section of my App Service for entries that differ between local environments and other environments.
An example of what I'm looking to accomplish:
Client side Blazor:
@functions {
//...more code here
await Http.PostJsonAsync<object>("http://localhost:50466/api/auth/register", vm);
}
on the deployed web server this should be transformed to:
@functions {
//...more code here
await Http.PostJsonAsync<object>("http://wwww.mywebsite.com/api/auth/register", vm);
}
So I'm looking for a way to store the site root url in an environment variable and transform it upon publish. Is there a Blazor-ey way to do this?
Blazor is a tough sell to current web developer, because it means leaving behind many of the libraries and technologies that have been up over a decade of modern JavaScript development. It's not a seamless transition, and there's no way to migrate a JavaScript application to a Blazor project.
Blazor is very fast in terms of build and debugging. Because it takes advantage of Visual Studio and the whole experience accumulated among its tools and plugins, the development and debugging can be really fast.
First to access browser seesionStorage in Blazor apps, write a custom code or use a third party package. The accessed data can be stored in the localStorage and sessionStorage. The localStorage is scoped to the user's browser. If the user reloads the page or closes and reopens the browser, the state persists.
You can create singleton with configuration interface and inject it in you components.
.csproj
<ItemGroup>
<EmbeddedResource Include="appsettings.Development.json" Condition="'$(Configuration)' == 'Debug'">
<LogicalName>appsettings.json</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="appsettings.json" Condition="'$(Configuration)' == 'Release'">
<LogicalName>appsettings.json</LogicalName>
</EmbeddedResource>
</ItemGroup>
Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(GetConfiguration());
}
private IConfiguration GetConfiguration()
{
// Get the configuration from embedded dll.
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("appsettings.json"))
using (var reader = new StreamReader(stream))
{
return JsonConvert.DeserializeObject<IConfiguration>(reader.ReadToEnd());
}
}
MyComponent.razor
@inject Configuration.IConfiguration Configuration;
Or look this issue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With