TempData的状物体中的WebForms - 会话状态只有1征求意见函 [英] TempData Like Object in WebForms - Session State for only 1 Additional Request

查看:135
本文介绍了TempData的状物体中的WebForms - 会话状态只有1征求意见函的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过会话状态来存储一些对象的只为一个请求的。我似乎无法想到一个简单的方法来做到这一点。这是的究竟的什么ASP.NET MVC的TempData的对象一样。谁能给我提供一个链接或如何在会话状态的对象只能存活一个附加要求一些例子吗?

I would like to store some objects only for one request through the session state. I can't seem to think of an easy way to accomplish this. This is exactly what ASP.NET MVC's TempData object does. Could anyone provide me with a link or some examples of how to have an object in session state only survive one additional request?

我在想,这可以通过自定义词典对象,它存储在每个项目上的年龄(请求数)来完成的可能。通过订阅的Application_BeginRequest和Application_EndRequest方法,你可以执行的对象所需的清理工作。这甚至可能促进使所存储数据片段对于x请求,不只是一个对象。这是在正确的轨道上?

I was thinking, this could be possibly accomplished by making a custom dictionary object, which stores an age (# of requests) on each item. By subscribing to the Application_BeginRequest and Application_EndRequest methods, you could perform the required cleanup of the objects. This could even probably facilitate making an object that stored a piece of data for X requests, not just one. Is this on the right track?

推荐答案

我实现非常类似于您在Global.ascx.cs的Application_AcquireRequestState方法描述什么东西。我的所有会话对象被包裹在该保持读取的数目的计数的类

I implemented something very similar to what you describe in the Application_AcquireRequestState method of Global.ascx.cs. All my session objects are wrapped in a class that keeps count of the number of reads.

// clear any session vars that haven't been read in x requests
List<string> keysToRemove = new List<string>();
for (int i = 0; HttpContext.Current.Session != null && i < HttpContext.Current.Session.Count; i++)
{
    var sessionObject = HttpContext.Current.Session[i] as SessionHelper.SessionObject2;
    string countKey = "ReadsFor_" + HttpContext.Current.Session.Keys[i];
    if (sessionObject != null/* && sessionObject.IsFlashSession*/)
    {
        if (HttpContext.Current.Session[countKey] != null)
        {
            if ((int)HttpContext.Current.Session[countKey] == sessionObject.Reads)
            {
                keysToRemove.Add(HttpContext.Current.Session.Keys[i]);
                continue;
            }
        }
        HttpContext.Current.Session[countKey] = sessionObject.Reads;
    }
    else if (HttpContext.Current.Session[countKey] != null)
    {
        HttpContext.Current.Session.Remove(countKey);
    }
}

foreach (var sessionKey in keysToRemove)
{
    string countKey = "ReadsFor_" + sessionKey;
    HttpContext.Current.Session.Remove(sessionKey);
    HttpContext.Current.Session.Remove(countKey);
}

这篇关于TempData的状物体中的WebForms - 会话状态只有1征求意见函的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆