0

In ASP.NET Core there are html tags like

<environment include="Staging">THIS IS STAGING</environment>

But I'm a little confused working in framework, I was trying to do something like

<div class=@(System.Environment.GetEnvironmentVariable() == "Staging" ? "RED" : "GREY"")>

But realised an environment variable is a child of environment, and the above method is looking for a value by its key.

I'm not actually sure I want "environment" anyway... it's actually a Configuration defined in the solution configurations box?

And then I realised I don't want "if staging else" statements in my html, I just want different html deployed like I would with the different web.config files, so how do I tackle this issue in framework? Do I use transforms like the config files for each cshtml view?

1 Answer 1

1

As far as I know there is not a way to do what you are asking with ASP.NET Core without writing something custom (whether that is in the build process to run specific commands/scripts) or whether that is something that is maybe utilizing file names with deployment environments in the filename "ListOfEntities.staging.cshtml" and then again writing some custom code to choose the correct file to render based on a setting in the appsettings. I personally loved the web.config transforms but i haven't found a solution like that available in asp.net core. Even with the older web.config transforms though you wouldn't be including/excluding specific files on publish using the built in publish process. I think you would still have had to write something custom to do what you are wanting. The best thing i personally have ever come across is just sticking with the "if staging else" in the html like you were saying you were trying to avoid. It's intuitive for future developers to maintain even though it does seem like adding extra spaghetti in there.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I ended up adding a key in my web.configs which will transform per configuration, and just a couple of @if (System.Web.Configuration.WebConfigurationManager.AppSettings["Environment"] == "Release") lines in my _layout page. Basically just to highlight to users if we're in Staging mode etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.