• Session为空的一种原因


    在维护一份比较老的代码,想改为ajax调用,然后就添加了一个一般处理程序文件,也就是以.ashx结尾的文件,一切都正常,但发现session一直为空,很奇怪


    基本的代码如下:

    public class GetDataSurveyPerformance : IHttpHandler
    {
    
        private string OperationTypeList = "list";
        private string OperationTypeAdd = "add";
        private string OperationTypeDel = "del";
    
        public void ProcessRequest(HttpContext context)
        {
             HttpSessionState session = HttpContext.Current.Session;//为空,根本取不到存在Session中的数据
    		 
            string sourceurl2 = context.Request.UrlReferrer.LocalPath.ToString().ToLower().Trim();
            string type = context.Request.QueryString["OperationType"].ToLower().Trim();
            if (sourceurl2.EndsWith("SurveyPerformanceList.aspx".ToLower().Trim()))
            {
                SurveyPerformanceAction(context);
            }
            else if (sourceurl2.EndsWith("SurveyPerformanceadd.aspx".ToLower().Trim()))
            {
             SurveyPerformanceAction(context);
            }
        }
    }


    最后发现,在其他页面都可以取到session中保存的内容,只有此处为空,很奇怪。经过很长时间的测试,解决方法竟然也很简单,就是还需要继承一个接口IRequiresSessionState

    也就是改为如下代码就可以解决了:

    public class GetDataSurveyPerformance : IHttpHandler, IRequiresSessionState//多了一个接口IRequiresSessionState
    {
    
        private string OperationTypeList = "list";
        private string OperationTypeAdd = "add";
        private string OperationTypeDel = "del";
    
        public void ProcessRequest(HttpContext context)
        {
             HttpSessionState session = HttpContext.Current.Session;//现在不为空了,能取到存在Session中的数据
    		 
            string sourceurl2 = context.Request.UrlReferrer.LocalPath.ToString().ToLower().Trim();
            string type = context.Request.QueryString["OperationType"].ToLower().Trim();
            if (sourceurl2.EndsWith("SurveyPerformanceList.aspx".ToLower().Trim()))
            {
                SurveyPerformanceAction(context);
            }
            else if (sourceurl2.EndsWith("SurveyPerformanceadd.aspx".ToLower().Trim()))
            {
             SurveyPerformanceAction(context);
            }
        }
    }



  • 相关阅读:
    解决RobotFramework的关键字不能高亮的问题
    使用Python遇到:'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte 问题
    通过Jekins执行bat脚本始终无法完成
    Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
    [转]The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    HDU 2686 MCMF
    HDU 4278 卡特兰,区间DP
    POJ 2985 名次树
    POJ 2531 深搜剪枝
    Uva 10061 进制问题
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3157120.html
Copyright © 2020-2023  润新知