I have been trying to implement the http context in the ASP.NET MVC and system.web ,it would just use allow me to use HttpContext.Current to access the context . Anyway I started by injecting the IhttpcontextAccessor in the configureService method in the StartUp Class. I am posting this to see if anyone has implemented this using .Net Core 2.0. If so, please feel free to share the knowledge. Thanks in advance.
services.AddSingleton<HttpContextAccessor, HttpContextAccessor>();
If you need it from a Controller, then just use
HttpContext, as Muqeet's answer says.If you do need to inject it, then you're on the right track with
AddSingleton, but you have a slight typo:Notice the
Iin the first type.Then set up your constructor:
Then you can use
contextanywhere else in your class.That's the basics of dependency injection.
AddSingletonmakes it available for injection. Then you ask for it where you need it by adding it to your constructor.That article that Maarten linked to in his comment explains in more detail.