Can I use the HttpContext.ApplicationInstance.Context
class to store session data using the Session
function?
Or there is a better way to do this?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- MVC-Routing,Why i can not ignore defaults,The matc
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
Yes, you can apply data into a Session by utilizing the
HttpContext
. You should be wary of such implementations though, Model View Controller is in nature stateless. The session though will dictate some form of state.You'll have to account for that, otherwise you could potentially introduce a large quantity of orphaned session variables if they're not accounted for. Which could eat memory in your environment rapidly depending on your application.
If the application is small, you could easily do it with the following in your Controller:
Normally the session is available as a property in your controller actions:
Since the
Session
property is of typeHttpSessionStateBase
it can be more easily mocked in a unit test. Please never use the staticHttpContext.Current.Session
as you might see suggested elsewhere.If you are not inside a controller action you can access the session if you have an instance of
HttpContextBase
(which is almost in every part of the MVC pipeline) using its Session property.