2011-07-05 53 views
4

我正在创建一个http模块,我想检查一个请求是否来自经过身份验证的用户,如果不是,则重定向到登录页面。http模块:请求不可用

我注册在web.config文件中的模块,我有以下代码的投掷一个例外:

public class IsAuthModule : IHttpModule 
{ 
    public void Dispose() { } 

    public void Init(HttpApplication TheApp) 
    { 
     var TheRequest = TheApp.Request; 

    } 
} 

它扔,说一个例外“的要求是不是在这方面提供”

我在做什么错?

+0

http://stackoverflow.com/questions/2518057/request-is-not-available-in-this-context – rahularyansharma

回答

5

在Init阶段,您没有正在进行的请求。您需要申请开通该事件的请求开始:

public void Init(HttpApplication TheApp) 
{ 
    TheApp.BeginRequest += Application_BeginRequest; 

    // End Request handler 
    //application.EndRequest += Application_EndRequest; 
} 

private void Application_BeginRequest(Object source, EventArgs e) 
{ 
    // do something 
} 
+1

感谢这个漂亮的回答言简意赅,肯定会跳过教程长度的解释! – frenchie