I am writing a web application that will allow a user to browse to multiple web pages within the website making certain requests. All information that the user inputs will be stored in an object that I created. The problem is that I need this object to be accessed from any part of the website and I don't really know the best way to accomplish this. I know that one solution is to use session variables but I don't know how to use them in asp .net MVC. And where would I declare a session variable? Is there any other way?
相关问题
- MVC-Routing,Why i can not ignore defaults,The matc
- parameters in routing do not work MVC 3
- Data loss during sending via $_SESSION from one sc
- Chrome not keeping my _SESSION vars when coming fr
- Using a session with php and Java
相关文章
- Page指令 的EnableSessionState="ReadOnly",怎么在web.confi
- How to get a list of connected clients on SignalR
- How do you redirect to the calling page in ASP.NET
- Change color of bars depending on value in Highcha
- The program '[4432] iisexpress.exe' has ex
- How exactly do Firebase Analytics handle session d
- ASP.Net MVC 4 Bundles
- How to get server path of physical path ?
Because I dislike seeing "HTTPContext.Current.Session" about the place, I use a singleton pattern to access session variables, it gives you an easy to access strongly typed bag of data.
then you can access your data from anywhere:
My way of accessing sessions is to write a helper class which encapsulates the various field names and their types. I hope this example helps:
I would think you'll want to think about if things really belong in a session state. This is something I find myself doing every now and then and it's a nice strongly typed approach to the whole thing but you should be careful when putting things in the session context. Not everything should be there just because it belongs to some user.
in global.asax hook the OnSessionStart event
From anywhere in code where the HttpContext.Current property != null you can retrive that object. I do this with an extension method.
This way you can in code
You can use ViewModelBase as base class for all models , this class will take care of pulling data from session
You can write a extention method on HttpContextBase to deal with session data
Use this like below in controller
The second argument is optional it will be used fill session data for that key when value is not present in session.
Although I don't know about asp.net mvc, but this is what we should do in a normal .net website. It should work for asp.net mvc also.
You would put this inside a method for easy access. HTH
Great answers from the guys but I would caution you against always relying on the Session. It is quick and easy to do so, and of course would work but would not be great in all cicrumstances.
For example if you run into a scenario where your hosting doesn't allow session use, or if you are on a web farm, or in the example of a shared SharePoint application.
If you wanted a different solution you could look at using an IOC Container such as Castle Windsor, creating a provider class as a wrapper and then keeping one instance of your class using the per request or session lifestyle depending on your requirements.
The IOC would ensure that the same instance is returned each time.
More complicated yes, if you need a simple solution just use the session.
Here are some implementation examples below out of interest.
Using this method you could create a provider class along the lines of:
And register it something like: