-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What is the alternative to get the HttpContext.Current in .net core ? #689
Comments
In what context? There are several ways to get it - you can't grab it statically anymore. |
The correct (and only) way to really do this is to inject the HttpContextAccessor into the class or service you want to use it in. |
This issue is probably better to open on the aspnet/home repo where more people that know the details of HttpContext would be watching. |
@Eilon do you know who could help here? |
This should be able to get you in the right direction. https://www.strathweb.com/2016/12/accessing-httpcontext-outside-of-framework-components-in-asp-net-core/ |
That linked blog post has the answer. However, in general we don't recommend using that pattern at all. Any component that needs to know the current HttpContext needs to have the HttpContext passed to it - the same as you would do for any other piece of data that the component needs. |
Please continue the conversation on the aspnet/home repo if you need more information. Thanks. |
@Eilon so for the sake of clarity, suppose we have a repository class, that neesd access to HttpContext.Current , we would first inject HttpContextAccessor in the controller calling that class, and then pass reference to context to the class from controller correct? |
@naveedahmed1 yes that sounds like it would work. However, I think in common/popular app architectures a "repository" class generally doesn't need access to |
Thank you so much @Eilon for quick reply, sorry I meant a utility class but got your point. |
Any one can suggest how to use the HttpContext.Current in .net core ?
The text was updated successfully, but these errors were encountered: