保留HttpModule中的值

时间:2015-03-12 02:31:09

标签: httpmodule

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

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

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

1 个答案:

答案 0 :(得分:0)

只需添加一个静态属性。

例如:

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.

    }

    //...

}

相关问题