Skip to content

Instantly share code, notes, and snippets.

@conficient
Created July 30, 2020 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conficient/aca15e92931a15d76047cee598df2e0c to your computer and use it in GitHub Desktop.
Save conficient/aca15e92931a15d76047cee598df2e0c to your computer and use it in GitHub Desktop.
Blazor DevOnly component - only show the content of the component in Development mode
@using Microsoft.Extensions.Hosting
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment env
@if (isDevelopment)
{
@ChildContent
}
@code {
[Parameter] public RenderFragment ChildContent { get; set; }
bool isDevelopment = false;
protected override void OnInitialized()
{
isDevelopment = env.IsDevelopment();
}
}
@daaa57150
Copy link

daaa57150 commented Oct 25, 2022

Dang, I was just starting to write a 'DevOnly' component, (same name exactly!), and was wondering if I could inject IWebHostEnvironment.
Thanks :)

Edit:
In WASM I needed to inject this instead, but it works all the same:

@using Microsoft.AspNetCore.Components.WebAssembly.Hosting 
@inject IWebAssemblyHostEnvironment env

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment