在ASP.NET MVC的每个请求执行code [英] Execute code on every request in ASP.NET MVC

查看:106
本文介绍了在ASP.NET MVC的每个请求执行code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器,我所有的控制器继承,我需要执行一些code为每个控制器的请求。我试过如下:

I have a controller that all my controllers inherit from and I need to execute some code for every controller request. I tried the following:

protected override void Execute(System.Web.Routing.RequestContext requestContext)
{
    if (Session["mallDetected"] == null)
    {
        Session["mallDetected"] = DateTime.Now.Ticks;
        IList<Mall> malls = Mall.FindNearestByIp(Request.UserHostAddress);

        if (malls.Count > 0)
        {
            Session["mall"] = malls[0];
        }
    }

    base.Execute(requestContext);
}

但显然会话状态不是Execute方法可用,直到调用base.Execute(),它不为我工作了。有没有在那里我可以执行此届code在ASP.NET MVC?

But apparently session state isn't available in the Execute method until AFTER the call to base.Execute(), which doesn't work for me. Is there a place where I can execute this session code for each request in ASP.NET MVC?

推荐答案

尝试重写 OnActionExecuted 在基本控制器或实现的功能的过滤器。

Try overriding OnActionExecuted in the base controller or implementing the functionality as a filter.

这篇关于在ASP.NET MVC的每个请求执行code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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