开发者

problem with HttpContext in mvc 2

开发者 https://www.devze.com 2023-03-22 06:03 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消