Redirect to specific page after session expires (MVC4)

后端 未结 3 1037
花落未央
花落未央 2020-12-05 08:26

C# MVC4 project: I want to redirect to a specific page when the session expires.

After some research, I added the following code to the Global.asax in m

3条回答
  •  旧时难觅i
    2020-12-05 08:48

    create this action filter class

        class SessionExpireAttribute : ActionFilterAttribute {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext.HttpContext.Session["logged"] == null)
            {
                filterContext.Result = new RedirectResult("/Account/Login");
            }
            base.OnActionExecuted(filterContext);
        }
    

    then use it in your class or method like bellow

    [SessionExpireAttribute]
    public class MyController : Controller
    {
        ....
    }
    

提交回复
热议问题