5 回答

TA貢獻1799條經(jīng)驗 獲得超8個贊
不都一樣么?
使用方法都是一樣的。沒有差別。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write("Hello World");
HttpCookie cookie = new HttpCookie("Test");//初使化并設(shè)置Cookie的名稱
TimeSpan ts = new TimeSpan(0, 0, 1, 0, 0);//過期時間為1分鐘 cookie.Expires = DateTime.Now.Add(ts);//設(shè)置過期時間 cookie.Values.Add("userid", "123456"); cookie.Values.Add("test", "THIS_IS_TEST"); context.Response.AppendCookie(cookie);
context.Response.Write(context.Request.Cookies["Test"].Value); } |

TA貢獻1844條經(jīng)驗 獲得超8個贊
HttpCookie cookie = HttpContext.Current.Request.Cookies["info"];
// cookie = null;
if (cookie == null )
{
cookie = new HttpCookie("Info");
cookie["CityID"] = HttpContext.Current.Server.UrlEncode(cityID);
cookie["CityName"] = HttpContext.Current.Server.UrlEncode(CityName);
cookie.Expires = DateTime.Now.AddDays(10);//
HttpContext.Current.Response.Cookies.Add(cookie);
}else{
//直接讀值,注意編碼 解碼、不然漢字會出現(xiàn)亂碼。
}

TA貢獻1813條經(jīng)驗 獲得超2個贊
.net的一般處理程序 .ashx的context對象默認(rèn)是取不出session的值出來的。
要達到取出Session的效果,則需要讓它實現(xiàn)System.Web.SessionState.IReadOnlySessionState接口(該接口沒有任何方法實現(xiàn),只是起到一個標(biāo)識作用)
為了讓所有的一般處理程序都能獲取到Session值,并且能集中做一些控制管理(比如用戶認(rèn)證、權(quán)限控制等),我的策略是讓一個抽象類實現(xiàn)IHttpHandler, IRequiresSessionState接口,然后讓其他所有一般處理程序都繼承該抽象類即可。
- 5 回答
- 0 關(guān)注
- 636 瀏覽
添加回答
舉報