检查所有控制器的会话? [英] Check Session for all Controllers?

查看:55
本文介绍了检查所有控制器的会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户登录后,将设置一个会话.我想检查所有控制器中的用户会话(不是全部,而是需要登录用户的控制器).在Webforms中,我们可以在page_load或使用基类来完成.在asp mvc中推荐的方法是什么? (我看到使用基本控制器是个坏主意)

After user logged in, a session will be set. I want to check the user session in all controllers (not all, but controllers which require a logged in user). In webforms we could do it in the page_load or using base classes. What's the recommended approach in asp mvc to do this? (I saw it would be a bad idea to use base controllers)

推荐答案

您可以通过base controller创建带有继承的控制器(在此检查会话),并将该控制器引用到所需的控制器中.

You can create a controller (check session in this) with inherit by base controller and refer this controller into your required controller.

BaseController:

BaseController:

 public class BaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //check Session here
        }
    }

DerivedController:

DerivedController:

 public class ABCController : BaseController
    {
       //Your code
    }

这篇关于检查所有控制器的会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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