0

我正在尝试访问自定义类中的会话变量,如下所示:

控制器:

    public void RemoveMachinesListChanged(object sender, RemovePackageEvent e)
    {
        PackageActionStatus.Current.Machines = e.Machines;
        // Dictionary<int, int> machines =  e.Machines;
    }

public class PackageActionStatus
{
    public static PackageActionStatus Current
    {
        get
        {
            HttpContextWrapper context = new HttpContextWrapper(System.Web.HttpContext.Current);
            var session = (PackageActionStatus)context.Session["PackageActionStatus"];
            if (session == null)
            {
                session = new PackageActionStatus();
                context.Session["PackageActionStatus"] = session;
            }

            return session;
        }
    }

    private Dictionary<int, int> machines;

    public Dictionary<int, int> Machines
    {
        get
        {
            return this.machines;
        }
        set
        {
            if (this.machines == null)
            {
                this.machines = new Dictionary<int, int>();
            }

            this.machines = value;
        }
    }
}

我在这里想要实现的是通过 AJAX 调用从/向会话变量存储和检索值。似乎每当我使用ajax HttpContext 时都是空的。我该怎么做?谢谢

4

1 回答 1

0

HttpContext 在请求生命周期的某些点可用。此外,它仅在请求的线程中可用。

实现一个IHttpHandler具有ProcessRequest请求实例作为输入参数的方法(在 MSDN http://msdn.microsoft.com/en-us/library/system.web.ihttphandler.aspxHttpContext上了解更多信息)。

在此处检查ProcessRequest示例实现:http: //msdn.microsoft.com/en-us/library/system.web.ihttphandler.processrequest.aspx

于 2012-11-12T10:33:54.473 回答