zoukankan      html  css  js  c++  java
  • 如何在ashx页面获取Session值

    在一般事务处理页面,可以轻松的得到 Request,Response对象,从而进行相应的操作,如下:

    1 HttpRequest Request = context.Request; 
    2 HttpResponse Response = context.Response;

    但是要得到 Session的值就没有那么简单了。比如你要在ashx得到保存在Session中的登录帐号Session["userAccount"]

    如果你只是context.Session["userAccount"]的话是会报 “未将对象引用设置到对象的实例”的异常

    所以,如果要想取Session中的值 ,需要如下所示

    1、引入 命名空间:

    1 using System.Web.SessionState;

    2、实现IRequiresSessionState接口,具体如下  

     1 /// <summary>
     2     /// $codebehindclassname$ 的摘要说明
     3     /// </summary>
     4     [WebService(Namespace = "http://tempuri.org/")]
     5     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
     6     public class AddUserInfo : IHttpHandler,IRequiresSessionState //就是这样显示的实现一下,不用实现什么方法
     7     {
     8 
     9         public void ProcessRequest(HttpContext context)
    10         {
    11 
    12       //...
    13 
    14        //这样你就可以如下 操作了
    15 
    16                 if(context.Session["userAccount"] != null)
    17 
    18       {
    19 
    20         string account = context.Session["userAccount"].ToString();
    21 
    22       }
    23 
    24       //...继续下面的代码
    25 
    26     }
    27 
    28   }
  • 相关阅读:
    model.object对象查询过滤、增删改、Q
    模板中的标签、过滤器
    模板(template)包含与继承
    url用法
    AD用户登录验证,遍历OU(LDAP)
    Python下操作sqlite3
    多线程应用-类(thread)
    数组(list)分组、分段
    多线程应用-函数方式(thread)
    IntelliJ IDEA maven项目 ***
  • 原文地址:https://www.cnblogs.com/soulmate/p/5283672.html
Copyright © 2011-2022 走看看