Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access env.IsDevelopment() from a Blazor page?

I'd like to use env.IsDevelopment() to control what content I see on a Blazor page.

I could add a singleton to the Startup class:

public static IWebHostEnvironment Env { get; private set; }

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    Env = env;
}

Or is there another way to access IWebHostEnvironment (or Startup) via an instance variable?

like image 836
Mitkins Avatar asked Dec 08 '22 11:12

Mitkins


1 Answers

Turns out that I can do this with injection. I added the following on my page:

@using Microsoft.AspNetCore.Hosting;
@using Microsoft.Extensions.Hosting;
@inject IWebHostEnvironment Env

Which allowed me to do stuff like this:

@if ( Env.IsDevelopment() )
{
    <div>Some content that I'm testing and don't want on the server yet</div>
}

Thanks @Nkosi!

like image 189
Mitkins Avatar answered Dec 10 '22 00:12

Mitkins