Skip to content

Instantly share code, notes, and snippets.

Created July 30, 2020 16:42
What would you like to do?
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)
@code {
[Parameter] public RenderFragment ChildContent { get; set; }
bool isDevelopment = false;
protected override void OnInitialized()
isDevelopment = env.IsDevelopment();
Copy link


   <p>Show some debugging info here!</p>

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 :)

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