ASP.NET MVC 不调用 global.asax; 结束请求

发布于 2024-07-17 12:20:16 字数 931 浏览 6 评论 0原文

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

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

lambda 不会被调用。 有什么想法吗?

编辑: 我看到他们在 SharpArch [http://code.google.com/ 中做了类似的事情p/sharp-architecture/] 它确实在那里工作...... 不,我不想使用 HttpModule。

编辑2: 我发现的唯一解决方法是将 Application_EndRequest 与 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);
}

wss 必须是私有的,因为看起来 Application_EndRequest 是使用不同的实例对象(this)调用的。 这也可能是我的活动(如开头所述)没有被召集的原因。

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?

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深者入戏 2024-07-24 12:20:16

我通常会这样做:

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

这按预期工作。 虽然不知道这个活动。

I usually do:

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

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

忘年祭陌 2024-07-24 12:20:16

global.asax 文件表示的 HttpApplication 实例是仅表示第一个 HttpApplication 对象的单个实例。 不保证 HttpApplication 的此实例将用于任何其他请求。

您需要重写 global.asax 中的 Init() 方法,并在该方法中挂钩您想要的任何事件:

public override void Init() {
    base.Init();

    EndRequest += MyEventHandler;
}

请参阅 这篇 MSDN 文章 了解有关 HttpApplication 对象的详细信息。

The HttpApplication instance that is represented by your global.asax file is a single instance that only represents the first HttpApplication object. It is not guaranteed that this instance of the HttpApplication will be used for any other request.

You need to override the Init() method in global.asax and in that method hook up any events that you want:

public override void Init() {
    base.Init();

    EndRequest += MyEventHandler;
}

Please refer to this MSDN article for more info on the HttpApplication object.

冷血 2024-07-24 12:20:16

最好的选择是在 HttpModule 中执行此操作。 我使用 HttpModule 来管理 MVC 应用程序中的 NHibernate 会话,它工作得很好。 在开始请求中,我将 sessionFactory 绑定到 ManagedWebSessionContext(在 NHibernate 中,但相当未记录),然后在结束请求中,我提交任何事务并取消绑定 sessionFactory。

我认为将其分离到 HttpModule 中也更干净。

Your best bet is to do this in an HttpModule. I use an HttpModule to manage NHibernate session in an MVC app and it works perfectly. In the begin request I bind the sessionFactory to the ManagedWebSessionContext (in NHibernate but fairly undocumented) and then in the end request I commit any transactions and unbind the sessionFactory.

I think it is cleaner to separate this into an HttpModule as well.

萝莉病 2024-07-24 12:20:16

对于ASP.NET MVC,您需要添加。

protected void Application_EndRequest()
{
    Debug.WriteLine("End Request");
}

For ASP.NET MVC, you need to add.

protected void Application_EndRequest()
{
    Debug.WriteLine("End Request");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文