在 MVC 操作过滤器中的会话超时后重定向 [英] Redirect after Session Timeout in MVC Action Filter

查看:66
本文介绍了在 MVC 操作过滤器中的会话超时后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在 mvc 5 应用程序中的会话超时后重定向到主页.我正在使用以下代码:

I have to redirect to home page after session timeout in mvc 5 application. I am using following code :

 public class SessionExpireAttribute : ActionFilterAttribute
    {

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            var currentEnvironment = EnvironmentUtility.GetSession().Get<Environment>();
            if (currentEnvironment == null)
            {
                filterContext.Result = new RedirectResult("~/Home/Index");
            }
            base.OnActionExecuting(filterContext);
        }

    }

在控制器中,我有以下代码:

In the controller, I have below code :

[SessionExpire]
public ActionResult Submit()
{
    var urlReferrer = this.Request.UrlReferrer;
    if (urlReferrer != null)
    {
     return this.Redirect(urlReferrer.ToString());
    }
    else
    {
        return View();
    }
}

点击提交按钮后,页面将使用javascript代码重定向到另一个页面,如下所示:

After the submit button is clicked, the page is redirecting to another page using javascript code as like below :

 function commitButtonClickedHandler()
{
       window.location.href = "@Url.Content("~/Working/NextPage")";
}

NextPage 如下所示:

And the NextPage is like below :

public ActionResult NextPage()
        {
            return this.View();
        }

会话超时后我的要求是什么,当您单击提交按钮时,它应该检查会话过期时间.如果它已过期,则应像在 SessionExpireAttribute 中一样将其重定向到 NextPage.否则它应该继续这个过程.但问题是会话超时后,页面仍然重定向到 NextPage() 操作.请帮我解决这个问题.谢谢.

What my requirement is after session timeout, when you click the submit button it should check seesion expiration. If it is expired , then it should be redirected to NextPage as like in SessionExpireAttribute. Else it should continue the process .But problem is after session timeout, the page is still redirecting to NextPage() action . Kindly help me to fix this. Thank you.

推荐答案

你可以通过覆盖 OnActionExecuting 来做一些改变,如下所示:

You can Do it by overriding OnActionExecuting making some change as below:

public class SessionExpireAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {            
            if ( Session["SomeSession"] == null)                    
            {
                //ClearAllSessions
                GoToLoginPage(filterContext, "You Session has been expired");
                return;
            }           
    }

    private static void GoToLoginPage(ActionExecutingContext filterContext, string message)
    {
        try
        {           


          filterContext.Result = new RedirectToRouteResult(
                 new RouteValueDictionary 
            { 
                { "controller", "controllerName" }, 
                { "action", "actionName" } ,
                { "Area","" },
                { "UnauthLogin",message },

            });
            }
        }
        catch
        {
            filterContext.Result = new RedirectToRouteResult(
                   new RouteValueDictionary 
            { 
                { "controller", "controllerName" }, 
                { "action", "actionName" } ,
                { "Area","" },
                { "UnauthLogin",message },

            });
        }
    }
 }

这篇关于在 MVC 操作过滤器中的会话超时后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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