Simon Ordo
Simon Ordo

Reputation: 1677

ASP.NET 5 (vNext) Environment Tag Helper From Code

I'm trying to determine how to get the functionalty of this tag helper in any class file:

<environment names="Development"> </environment>

I understand that environment information is available via an IHostingEnvironment object, but I'm not sure how to grab it without dependency injection.

Is it possible to get DI registered service implementations from any class?

In this specific case, I'm writing some HtmlHelper extension methods, but I think the question applies in a broader sense.

Upvotes: 1

Views: 251

Answers (1)

Simon Ordo
Simon Ordo

Reputation: 1677

So, from inside of an IHtmlHelper extension method, this works:

IHostingEnvironment env = HtmlHelper.ViewContext.HttpContext.RequestServices.GetService(typeof(IHostingEnvironment)) as IHostingEnvironment;

Is there a more generic way to grab a service instance without relying on dependency injection?

Upvotes: 1

Related Questions