dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.51k stars 10.04k forks source link

Provide a hosting model agnostic way of getting the current environemnt #48181

Open danroth27 opened 1 year ago

danroth27 commented 1 year ago

MVC & Razor Pages provide an environment tag helper for rendering different content based on the current environment. It doesn't look like we have a way to get the environment name in Blazor in a hosting model agnostic way. In Blazor WebAssembly apps you can find out the current environment using IWebAssemblyHostEnvironment.Environment. In Blazor Server apps you can find out the environment using IHostEnvironment.EnvironmentName. Perhaps we should add one?

javiercn commented 1 year ago

@danroth27 Would having an Environment component work?

javiercn commented 1 year ago

The other thing to account for here, is that I don't know how much this matters in webassembly scenarios, since the root component (app component) is always going to be statically rendered, I am not sure we need something that uses IWebAssemblyHostEnvironment.Environment as that is going to be relevant only when running in wasm, and its main use is to load the appropriate settings file.

dazinator commented 1 year ago

Off the top of my head, I'd be tempted to solve this in a different way that may be more flexibile / customisable?

Instead of using an Environment tag helper / style approach to do conditional rendering, another approach would be:

  1. Configure an Options class - i.e this will be bound to configuration thats loaded for the current environment (for web assembly hosting that would be based on IWebAssemblyHostEnvironment.Environment)

  2. Your options class could then have meaningful properties to control various features of your app for example: "ShouldRenderFeatureX"

  3. Additionally your options class could also have your own "EnvironmentName" configured for example "Dev"

You can now inject this options class wherever it's needed and do things ike

@if (EnvironmentOptions.EnvironmentName == "Dev")
{

}

@if (EnvironmentOptions.IsFeatureXEnabled)
{

}

This mechanism should work everywhere, it just all depennds on which config you bind the options class to.

danroth27 commented 1 year ago

@danroth27 Would having an Environment component work?

That was my initial thought as well. I'm not sure what it would do though in a Blazor Hybrid app.

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost commented 11 months ago

Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.