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


 


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM