会话到期时将“部分视图"重定向到登录页面 [英] Redirect Partial View to login page when session expires

查看:67
本文介绍了会话到期时将“部分视图"重定向到登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在会话期满后,是否有一种简单的方法可以将整个页面(而不仅仅是部分视图)重定向到登录页面?

我尝试了以下解决方案,但无法使其正常工作:

  • 解决方案

    我将以@EmilChirambattu的答案为基础.

      [HttpPost]公共ActionResult LogPartialView(字符串a,int?b,字符串c,字符串d,int?e,字符串f){//您应该先检查会话.如果(Session ["myID"] == null){返回ExpireSession();}//如果用户是管理员"->显示所有客户的日志.如果(Roles.IsUserInRole(WebSecurity.CurrentUserName,"Admin")){//一些代码}返回PartialView("LogPartialLayout",model);}公共无效ExpireSession(){Session.Abandon();WebSecurity.Logout();Response.Redirect("RedirectToLogin");}公共ActionResult RedirectToLogin(){返回PartialView("_ RedirectToLogin");} 

    _RedirectToLogin视图

     < script>window.location ='@ Url.Action("Index",")';</script> 

    这应该将您重定向到页面的基本URL(很可能是您的登录页面).

    Is there a simple way to redirect the entire page (not just the partial view) to the login page after the session has expired?

    I have tried the following solutions, but can't get it to work:

    My problem is that the partial view redirects to the Login-page, and not the entire page (same problem as in the links).

    Controller

            [HttpPost]
            public PartialViewResult LogPartialView(string a, int? b, string c, string d, int? e, string f)
            {
                //If the user is "Admin" -> display Logs for all customers.
                if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin"))
                {
                    if (Session["myID"] == null)
                    {
                        ExpireSession();
                    }
                //Some code
    
            return PartialView("LogPartialLayout", model);
            }
    

    I wanted to return Redirect ("~/") if myID is null but it doesnt work since it expects a Partial View.

    Error-message: Cannot implicitly convert type 'System.Web.Mvc.RedirectResult' to 'System.Web.Mvc.PartialViewResult'

    public void ExpireSession()
        {
            Session.Abandon();
            WebSecurity.Logout();
            Response.Redirect("~/");
    
        }
    

    解决方案

    I'm going to build on @EmilChirambattu s answer.

    [HttpPost]
    public ActionResult LogPartialView(string a, int? b, string c, string d, int? e, string f)
    {
        // You should check the session before anything else.
        if (Session["myID"] == null)
        {
            return ExpireSession();
        }
    
        //If the user is "Admin" -> display Logs for all customers.
        if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin"))
        {
            //Some code
        }
    
        return PartialView("LogPartialLayout", model);
    }
    
    public void ExpireSession()
    {
        Session.Abandon();
        WebSecurity.Logout();
        Response.Redirect("RedirectToLogin");
    }
    
    public ActionResult RedirectToLogin()
    {
        return PartialView("_RedirectToLogin");
    }
    

    _RedirectToLogin View

    <script>
        window.location = '@Url.Action("Index", "")';
    </script>
    

    This should redirect you to the base URL of the page (most likely your login-page).

    这篇关于会话到期时将“部分视图"重定向到登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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