【问题标题】:Retain value in HttpModule在 HttpModule 中保留值
【发布时间】:2015-03-12 02:31:08
【问题描述】:

我已经编写了自定义的 httpmodule,并且每次请求都会调用它。我想在我的第一个请求中存储该值,并且需要为后续请求检索该值。这在 HttpModule 中可行吗?

我知道 HttpContext.Current.Items 仅保留单个请求的值,我们不能根据我们的要求在项目中使用 Session。

除了 session\HttpContext.Current.Items 之外,我们还有其他选项可以在 HttpModule 中存储值吗?

【问题讨论】:

    标签: httpmodule


    【解决方案1】:

    只需添加一个静态属性。

    例如:

    public class TestModule : IHttpModule
    {
        private static string _myVal = null;
    
        public void Init(HttpApplication httpApplication)
        {
            httpApplication.BeginRequest += new EventHandler(this.OnBeginRequest);
    
            if (string.IsNullOrEmpty(_myVal)) {
                _myval = "whatever";
            }
    
            //that this is set... you can use it.
    
        }
    
        //...
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多