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);

        }

    }

}


 


 

你可能感兴趣的:(session)