| 類別 | 存放位置 | 有效範圍 | 生命週期 | 簡略說明 |
|---|---|---|---|---|
| Session | 伺服端 | 可跨頁面 | 預設20分鐘 | 1.佔用伺服器記憶體(使用上小心) 2.存放方式(webconfig設定):off、InProc、StateServer、SQLServer、Custom |
| Application state | 伺服端 | Web applicatio運行時間 | Web applicatio關閉或重啟 | 1.注意Web farm、Web garden狀況 |
● Session
![]() |
兩個Page
Login.aspx→輸入帳密送出,Return to LoginInfo.aspx Page。
LoginInfo.aspx→去取得Session資料,並回傳JSON,jQuery接收並顯示。
|
// Login.aspx 的按鈕事件
protected void LoginButton_Click(object sender, EventArgs e)
{
//Set Session
Session["UserID"] = this.AccountTextBox.Text;
Session["Password"] = this.PasswordTextBox.Text;
//Redirect → 重新導向URL
Response.Redirect("~/LoginInfo.aspx");
}
// LoginInfo.aspx 的AJAX Function
$.ajax({
type: "get",
url: "/LoginInfo.aspx/GetLoginInfo",
contentType: "application/json",
dataType: "json",
success: function (response) {
var info = JSON.parse(response.d);
$("#Accountlbl").text(info.UserID);
$("#Passwordlbl").text(info.Password);
}
})
/// <summary>
/// Session狀態機制範例練習,透過AJAX方法取得JSON資料。
/// 1. 方法需要改為 static
/// 2. 設置[WebMethod]
/// 3. 若method是get,必須設置[ScriptMethod(UseHttpGet = true)]
/// <summary>
/// <returns>回傳JSON</returns>
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static string GetLoginInfo()
{
Dictionary keyValuePairs = new Dictionary();
keyValuePairs.Add(
"UserID",
HttpContext.Current.Session["UserID"]
);
keyValuePairs.Add(
"Password",
HttpContext.Current.Session["Password"]
);
return Newtonsoft.Json.JsonConvert.SerializeObject(keyValuePairs);
}
Session WebConfig 設置
- off → 停止 Session 功能
- InProc(預設) → 背景工作處理序的處理序,IIS重啟會消失
- StateServer → 儲存獨立狀態下
- SQLServer → 儲存至SQL Server上
- timeout(預設20分) → 設置有效時間
- stateConnectionString → 若model="StateServer",設置連線字串
- stateNetworkTimeout(秒) → 若model="StateServer",設置web與狀態伺服器閒置的秒數
- stateConnectionString → 若model="SQLServer",設置連線字串
- stateNetworkTimeout(秒) → 若model="SQLServer",設置閒置的秒數
<sessionState mode="[Off|InProc|StateServer|SQLServer|Custom]" timeout="20" cookieless="[true|false|AutoDetect|UseCookies|UseUri|UseDeviceProfile]" stateConnectionString="tcpip=serverName:42424" stateNetworkTimeout="10" sqlConnectionString="data source=localhost;" sqlCommandTimeout="10" > </sessionState>
● Application state
protected void LoginButton_Click(object sender, EventArgs e)
{
Application["UserID"] = this.AccountTextBox.Text;
Application["Password"] = this.PasswordTextBox.Text;
//Redirect → 重新導向URL
Response.Redirect("~/LoginInfo.aspx");
}
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static string GetLoginInfo()
{
Dictionary keyValuePairs = new Dictionary();
keyValuePairs.Add(
"UserID",
HttpContext.Current.Session["UserID"]
);
keyValuePairs.Add(
"Password",
HttpContext.Current.Session["Password"]
);
keyValuePairs.Add(
"ApplicationUserID",
HttpContext.Current.Application["UserID"]
);
keyValuePairs.Add(
"ApplicationPassword",
HttpContext.Current.Application["Password"]
);
return Newtonsoft.Json.JsonConvert.SerializeObject(keyValuePairs);
}
假設UserID=123、Password=456,回傳如下:

沒有留言:
張貼留言