I am developing a Tag Helper in ASP.NET Core (RC2), and when rendering the Tag Helper I need to access the Request object as I need to figure out what the URL of the request is.
So it seems that in ASP.NET Core the correct way to access the Request object is from the HttpContext
, and to obtain the HttpContext
I need to inject IHttpContextAccessor
into my Tag Helper.
So I tried that but the following exception gets thrown at runtime:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting to activate 'Auth0.AspNetCore.Mvc.TagHelpers.LockTagHelper'.
Is there any reason I cannot inject IHttpContextAccessor
into my Tag Helper?
Also, is there perhaps another way to access the Request object inside a Tag Helper?
Edit
It seems the issue is that since the latest Release Candidates you have to manually configure the DI to handle IHttpContextAccessor. So in ConfigureServices
you have to make a call to
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
I believe in earlier betas this was automatically configured...
Problem is that this is a library, and I would prefer not to expect users to configure this in their application for my library to work correctly, so any other, more reliable way to access the Request object will still be appreciated :)