Search code examples
asp.net-core-2.1configurationmanagerasp.net-core-tag-helpers

How can I execute a TagHelper by Environment in ASP.NET Core?


I'm leaving todo: comments/notes/questions to for my UX designer and Product Owner in our Dev environment; I've created a TodoTagHelper that makes this really easy, e.g.:

<todo>need to make this a search field</todo>  

They like it, I like it, but I don't want to miss one and have it show up in Production; therefore, I'd like to change the output based on the environment.

How can I access the Configuration from within the TagHelper so I can remove the output if I'm not in Dev? My guess is to add it to the services collection in Startup.ConfigureServices, but I'm not sure what scope I should use... or even if it's already accessible somewhere else.


Solution

  • You should be able to inject IHostingEnvironment directly into your TodoTagHelper's constructor, and use that to get the current environment name (or check for Development by calling IHostingEnvironment.IsDevelopment()).