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 ?
The answer here is correct, I however struggled to implement it in an ASP.NET MVC 3 app. I wanted to access a Session object in a controller and couldn't figure out why I kept on getting a "Instance not set to an instance of an Object error". What I noticed is that in a controller when I tried to access the session by doing the following, I kept on getting that error. This is due to the fact that this.HttpContext is part of the Controller object.
However, what I wanted was the HttpContext that's part of the System.Web namespace because this is the one the Answer above suggests to use in Global.asax.cs. So I had to explicitly do the following:
this helped me, not sure if I did anything that isn't M.O. around here, but I hope it helps someone!
Well, IMHO..
With regards to #1, I have a strongly typed Master View which has a property to access whatever the Session object represents....in my instance the stongly typed Master View is generic which gives me some flexibility with regards to strongly typed View Pages
and then...
There are 3 ways to do it.
I prefer 3rd way.This link is good reference.
Get/Set HttpContext Session Methods in BaseController vs Mocking HttpContextBase to create Get/Set methods
If you are using asp.net mvc, here is a simple way to access the session.
From a Controller:
From a View:
This is definitely not the best way to access your session variables, but it is a direct route. So use it with caution (preferably during rapid prototyping), and use a Wrapper/Container and OnSessionStart when it becomes appropriate.
HTH