如何在APP_CODE类文件中访问会话变量?

时间:2013-06-07 09:05:51

标签: c# asp.net

我们有ASP.Net应用程序。 在Normal aspx.cs页面上,我可以访问这样的会话,

 Convert.ToString(Session[Resources.AMS.SESSION_USERID]);

但是在APP_CODE文件夹中,当我尝试时,我无法获得会话值,它给出了NULL !!

Convert.ToInt32(System.Web.HttpContext.Current.Session[Resources.AMS.SESSION_USERID])

需要做什么?

2 个答案:

答案 0 :(得分:0)

在app_code文件夹中的一个类中我有:

public static class SessionTest
{
    public static string OutputSession()
    {
        return System.Web.HttpContext.Current.Session.SessionID.ToString();
    }
}

在aspx.cs页面中,我有这个:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%
    Response.Write(SessionTest.OutputSession());
%>

输出会话ID。试试这个。

答案 1 :(得分:0)

仅该页面将设置Web上下文值。

App_Code是存储类文件的位置,请参见hereClass is a template for an object。您只能在运行时为其赋予价值。

您可以做的是向您的班级添加一个属性。

private int _sessionID;
public int SessionID
{
    get
    {
        return _sessionID;
    }
    set
    {
        sessionID = value;
    }
}

然后在您的代码后面(aspx.cs)

var myClass = new myClass();

myClass.SessionID = Convert.ToInt32(System.Web.HttpContext.Current.Session[Resources.AMS.SESSION_USERID])
相关问题