ASP.NET MVC PostAuthorizeRequest (and other events

2019-06-21 11:42发布

I'm working on the mvcForum project (on codeplex) and want to remove as much code as possible from the global.asax file - mostly to make it easier to integrate mvcForum into existing ASP.NET MVC application without changing too much code.

I need to hook into the application events to be able to set the correct CultureInfo (depending on the users' choice etc) and other things.

This isn't a problem with this in the global.asax file:

protected void Application_PostAuthorizeRequest() {
       // Some code here!
}

But when I try moving the code somewhere else, the event never happens. What I'm doing is this:

public MVCForumBootstrapper(HttpApplication app) {
    app.PostAuthorizeRequest += new EventHandler(app_PostAuthorizeRequest);
}

And this in the global.asax

    protected void Application_Start() {
      var strapper = new MVCForumBootstrapper(this);
    }

I was kind of expecting this to work in exactly the same way?

What am I doing wrong/have I missed?

Thanks, Steen

1条回答
神经病院院长
2楼-- · 2019-06-21 11:55

You should do this in the Init method of Global.asax. In Application_Start it's too late too hook events:

public override void Init()
{
    base.Init();
    var strapper = new MVCForumBootstrapper(this);
}
查看更多
登录 后发表回答