如何在MVC项目中会话到期后重定向同一页面?

时间:2018-03-12 05:50:04

标签: c# asp.net-mvc

    //I Have a Action Method 
    [HttpGet]
    public ActionResult Login()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Login(VmUser_User VmUser_User)
    {
        if (VmUser_User.User_User.UserName == null || 
        VmUser_User.User_User.Password == null)
        {
            VmUser_User.LblError = "Please enter Username and Password";
            return View(VmUser_User);
        }
        //Return valid user
        if (VmUser_User.LoginUser() > 0)
        {

            Session["One"] = VmUser_User;
            return RedirectToAction("Index", "Home");

        }
        else
        {
            VmUser_User.LblError = "User/Password does not match!";
        }

        return View(VmUser_User);
    }

   //And another Action Method
    public async Task<ActionResult> Common_Unit()
    {
        Oss.Romo.ViewModels.User.VmUser_User user = 
        (Oss.Romo.ViewModels.User.VmUser_User)Session["One"];
        if (user == null)
        {
            return RedirectToAction("Login", "Home");
        }
        vmCommon_Unit = new VmCommon_Unit();
        await Task.Run(() => vmCommon_Unit.InitialDataLoad());

        return View(vmCommon_Unit);
    } 

当有效的用户登录应用程序重定向到Home / Index页面时,他会请求Common / Common_Unit页面。在会话到期后,用户重新登录我想在最后请求的页面中重定向的应用程序,如Common / Common_Unit,请有人帮我解决这个问题。

我的问题:当授权用户浏览特定页面时,他会暂停一段时间。在最小时间会话中发生并且用户转到登录页面。登录后,我想在此特定页面上重定向用户。对不起我的坏英语

1 个答案:

答案 0 :(得分:0)

尝试使用ReturnUrl参数,如下所示:

    [HttpGet]
    public ActionResult Login()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Login(VmUser_User VmUser_User)
    {
        if (VmUser_User.User_User.UserName == null ||
        VmUser_User.User_User.Password == null)
        {
            VmUser_User.LblError = "Please enter Username and Password";
            return View(VmUser_User);
        }
        //Return valid user
        if (VmUser_User.LoginUser() > 0)
        {

            Session["One"] = VmUser_User;

            if (Request.QueryString["ReturnUrl"] != null & Request.QueryString["ReturnUrl"] != "")
            {
                Response.Redirect(Request.QueryString["ReturnUrl"]);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }

        }
        else
        {
            VmUser_User.LblError = "User/Password does not match!";
        }

        return View(VmUser_User);
    }

    //And another Action Method
    public async Task<ActionResult> Common_Unit()
    {
        Oss.Romo.ViewModels.User.VmUser_User user =
        (Oss.Romo.ViewModels.User.VmUser_User)Session["One"];
        if (user == null)
        {
            return RedirectToAction("Login", "Home", new { ReturnUrl = "/Common/Common_Unit" });
        }
        vmCommon_Unit = new VmCommon_Unit();
        await Task.Run(() => vmCommon_Unit.InitialDataLoad());

        return View(vmCommon_Unit);
    } 
相关问题