ASP.NET MVC不调用Global.asax的'EndRequest [英] ASP.NET MVC doesn't call global.asax' EndRequest

查看:593
本文介绍了ASP.NET MVC不调用Global.asax的'EndRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每一个请求结束执行一些操作。
我换了创建新项目时进行测试时生成的Application_Start():

I am trying to perform some actions at the end of every request. I changed the Application_Start() that is generated when created new project to make a test:

protected void Application_Start()
{
    EndRequest += (s, e) =>
    {
        Console.Write("fghfgh");
    };
    RegisterRoutes(RouteTable.Routes);
}

该拉姆达不会被调用。任何想法,为什么?

The lambda won't get called. Any ideas why?

编辑:
我看到他们在做类似的事情在SharpArch [HTTP://$c$c.google.com/p/sharp-architecture/],它并在那里工作...
不,我不希望使用一个HttpModule。

edit: I see that they are doing similar thing in SharpArch [http://code.google.com/p/sharp-architecture/] and it does work there... And no, I don't want to use an HttpModule.

EDIT2:
我发现的唯一的解决方法是联合使用以使用Application_EndRequest与Global.asax中的私有静态成员:

edit2: The only workaround I found is to use Application_EndRequest in conjuction with a private static member of global.asax:

private static WebSessionStorage wss;
protected void Application_Start()
{
    //...
    wss = new WebSessionStorage(this);
    //...
}

protected void Application_EndRequest(object sender, EventArgs e)
{
    wss.EndRequest(sender, e);
}

,因为它看起来像正在使用不同的实例对象(本)称为Application_EndRequest WSS必须是私有的。这也可能是我的事件(如开头所述)没有被调用的原因。

wss must be private because it seems like the Application_EndRequest is being called using different instance object (this). That may also be reason of my event (as described at the beginning) not being called.

推荐答案

我通常做的:

protected void Application_EndRequest(object sender, EventArgs e)
{
}

这正常工作。不知道,虽然该事件。

This works as expected. Don't know about the event though.

这篇关于ASP.NET MVC不调用Global.asax的'EndRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆