HttpContext.Current.Session 和 Session 的區別

來源:互聯網
上載者:User

標籤:session失效   號碼   http   上下文   組件   load   串連   pre   打電話   

Session(會話)通常指一個動作從開始到結束不間斷的一個動作。

例如“打電話”,通常是“1.拿起電話--2.撥對方號碼--3.對方--4.掛機”。這四個步驟從完成到結束組成了一個基本的Session,中間任何一步斷裂,都會導致Session的失效。

而在瀏覽器裡,Session主要通過串連傳遞,“開啟購物--點擊串連選擇物品--添加到購物車--結賬”組成了一個Session,在不使用Cookie的情況下,中間任何一步斷裂都會Session失效。

所有,你用瀏覽器開啟2個頁面,在一個頁面裡賦值,在另外一個瀏覽器取值,是取不到的。只能在一個瀏覽器通過串連傳遞(或者通過代碼跳轉到另外一個頁面)而取到值。

 

HttpContext.Current.Session 和 Session主要是針對瀏覽器使用者,所有,基本上兩者基本上沒有差別,但是不是所有程式都是通過瀏覽器訪問的。

例如使用者通過exe程式擷取資料,就可能失效。

另外HttpContext.Current.是針對目前使用者,而Session則是針對的web上下文環境裡。

 

例如有2個頁面:default.aspx代碼如下

protected void Page_Load(object sender, EventArgs e)
{

System.Threading.Thread threadHand1 = new System.Threading.Thread(() =>        {            Session["a"] = "a";         });        threadHand1.Start();                Response.Redirect("default2.aspx");
}

  

而在另外一個頁面擷取

protected void Page_Load(object sender, EventArgs e)    {        Response.Write(Session["a"]);    }

  此時是可以擷取到的。

而如果上面代碼修改為

protected void Page_Load(object sender, EventArgs e)    {            System.Threading.Thread threadHand1 = new System.Threading.Thread(() =>        {           HttpContext.Current.Session["a"] = "a";         });        threadHand1.Start();                         Response.Redirect("default2.aspx");    }

另外一個頁面修改為

 protected void Page_Load(object sender, EventArgs e)    {        Response.Write(HttpContext.Current.Session["a"]);    }

  則擷取不到。

因為 default1.aspx裡的 System.Threading.Thread 啟動的Context並不是default2.aspx裡的Context。

總之,除非你開房exe,第三方組件介面等,否則,基本上2者基本上沒有區別。

HttpContext.Current.Session 和 Session 的區別

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.