0

I have a class library which is working on .net MVC. Now I need to migrate that library to .net core library and their is a class that is consuming

HttpContext.Current

(from System.Web namespace)
In .net core HttpContext.Current cannot be utilized.
So how can I get current context in my self contained class.
Even we can use IHttpContextAccessor with DI but as class library is not having any startup kind of files in it for initializing purpose I can't use this as it will give me null.

Dhiren
  • 153
  • 1
  • 13
  • it may be duplicate but I am not asking question regarding using httpcontext in class library but using in class that is independent in class library. – Dhiren Sep 03 '19 at 08:20
  • The accepted answer on that duplicate IS the answer. Use DI to configure the IHttpContextAccessor (and your class you want to inject into) and inject it into your class' constructor.. Bingo. - Remember of course, without a Web/WebApi front end, you wont have an HttpContext anyway - so if you're looking for a truly seperate solution - there isnt one – Robert Perry Sep 03 '19 at 09:15
  • @RobertPerry thanks for the answer but actually in my class library I have filters in which I can access actioncontext and then many internal properties and methods are usable to me but this class is actually fully independent in that class library only and in old mvc project this file have access to httpcontext easily but in .net core it is not possible to access and I need not have to change the code much – Dhiren Sep 03 '19 at 09:38
  • I think this is not a duplicate. The OP is asking about a .NET Core class library where IHttpContextAccessor doesn't even exist. The other question is about ASP.NET Core. I had a problem like the one in this question and I solved it by adding this to the .csproj file: – Marques Jan 15 '20 at 23:00

0 Answers0