【问题标题】:Event in Global.asax file that fires only for onceGlobal.asax 文件中仅触发一次的事件
【发布时间】:2012-10-03 09:35:52
【问题描述】:

在 MVC Global.asax 文件中,我们可以看到 Application_Start,该事件仅触发一次。但是该会话在此处尚未激活/可用。所以我的问题是,Global.asax 文件中是否有任何事件只触发一次并且会话也可用?
我问这个的原因是因为,我使用ExpandoObject
例如:

    public static dynamic Data
    {
        get
        {
            #region FAILSAFE
            if (HttpContext.Current.Session[datakey] == null)
            {
                HttpContext.Current.Session[datakey] = new ExpandoObject();
            }
            #endregion

            return (ExpandoObject)HttpContext.Current.Session[datakey];
        }
    }  

我想一次用 null 值初始化我的所有 ExpandoObject:

MyExpando.Data.UserInformation = null;  
MyExpando.Data.FolderInformation = null;

这就是为什么我要寻找一个只触发一次的事件。

【问题讨论】:

    标签: c# asp.net-mvc events


    【解决方案1】:
    protected void Session_Start(object sender, EventArgs e)
    {
        // Will fire once when a new user session is created.
    
        // Contrary to Application_Start this event could run multiple
        // times during the AppDomain lifetime but that will ensure you 
        // that all users have the required data stored into the session.
    }
    

    【讨论】:

      【解决方案2】:

      它是您正在寻找的会话开始事件..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-21
        • 2020-12-11
        • 1970-01-01
        • 1970-01-01
        • 2011-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多