Set Global Variable for Duration of a Request

前端 未结 1 636
礼貌的吻别
礼貌的吻别 2021-01-05 07:30

Can I set some kind of global variable for the length of a single Request, so that all the controls of the page can respond to it without having to pass it through to each o

相关标签:
1条回答
  • 2021-01-05 07:56

    Yes, you can. Look at the obvious place: the HttpContext and the HttpContext.Current.Items collection that is always accessible during request handling (see http://msdn.microsoft.com/en-us/library/system.web.httpcontext.items.aspx).

    Just as an hint:

    public static class RequestScopedData
    {
        private const string key = "key_that_you_choose";
        public static bool IsSaving
        {
            get
            {
                object o = HttpContext.Current.Items[key];
                return Convert.ToBoolean(o);            
            }
            set
            {
                HttpContext.Current.Items[key] = value;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题