如何在ashx页面获取Session值 (仅供个人参考)
在一般事務(wù)處理頁(yè)面,可以輕松的得到 Request,Response對(duì)象,從而進(jìn)行相應(yīng)的操作,如下:
HttpRequest Request = context.Request;?
HttpResponse Response = context.Response;
但是要得到 Session的值就沒(méi)有那么簡(jiǎn)單了。比如你要在ashx得到保存在Session中的登錄帳號(hào)Session["userAccount"]
如果你只是context.Session["userAccount"]的話是會(huì)報(bào) “未將對(duì)象引用設(shè)置到對(duì)象的實(shí)例”的異常
所以,如果要想取Session中的值 ,需要如下所示
1、引入 命名空間:
using System.Web.SessionState;
2、實(shí)現(xiàn)IRequiresSessionState接口,具體如下??
??? /// <summary>
??? /// $codebehindclassname$ 的摘要說(shuō)明
??? /// </summary>
??? [WebService(Namespace = "http://tempuri.org/")]
??? [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
??? public class AddUserInfo : IHttpHandler,IRequiresSessionState?//就是這樣顯示的實(shí)現(xiàn)一下,不用實(shí)現(xiàn)什么方法
??? {
??????? public void ProcessRequest(HttpContext context)
??????? {
//...
? //這樣你就可以如下 操作了
????????????????if(context.Session["userAccount"] != null)
{
string account = context.Session["userAccount"].ToString();
}
//...繼續(xù)下面的代碼
}
轉(zhuǎn)載于:https://www.cnblogs.com/nameisxuhui/archive/2012/05/17/2506103.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的如何在ashx页面获取Session值 (仅供个人参考)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: html跨浏览器兼容性问题
- 下一篇: 用户代理de疑惑