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

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

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

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

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

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

    1、引入 命名空间:

    using System.Web.SessionState;

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

        

    /// <summary>
        /// $codebehindclassname$ 的摘要说明
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        public class AddUserInfo : IHttpHandler,IRequiresSessionState //就是这样显示的实现一下,不用实现什么方法
        {
    
            public void ProcessRequest(HttpContext context)
            {
    
          //...
    
           //这样你就可以如下 操作了
    
                    if(context.Session["userAccount"] != null)
    
          {
    
            string account = context.Session["userAccount"].ToString();
    
          }
    
          //...继续下面的代码
    
        }
    
      }
  • 相关阅读:
    c语言 判断文件是否存在
    lua 二进制函数使用
    linux sort 多列正排序,倒排序
    free命令学习 输出理解
    nginx 配置实现逻辑预算
    nginx 使用ctx实现数据共享,修改上下文
    lua中的数学库
    tornado文件上传实例
    ajax技术初识与应用
    web框架--XSS攻击和CSRF请求伪造
  • 原文地址:https://www.cnblogs.com/qxpgwww/p/2630197.html
Copyright © 2011-2022 走看看