ASP.NET MVC PostAuthorizeRequest (and other events) not firing

允我心安 提交于 2019-12-13 13:07:29

问题


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:


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);
}


来源:https://stackoverflow.com/questions/5974673/asp-net-mvc-postauthorizerequest-and-other-events-not-firing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!