i get an error when i try t开发者_如何转开发o access HttpContext.Current from a controller. I want to get the session to add some session specific data, and this gives me a problem:
var a = HttpContext.Current.Session;
The error, allegedly is that 'System.Web.HttpContextBase' does not contain a definition for 'Current'.
when i try to access HttpContext.Current from a controller
You should never use HttpContext.Current
.
Simply use the Session
property to access the session:
public ActionResult Index()
{
Session["foo"] = "bar";
...
}
The reason for the error you are getting is because the controller class already has a property called HttpContext so when you write HttpContext.Current
it is the property that is used and not the static Current
property on the HttpContext
class.
精彩评论