0

在我们的 MVC 应用程序中,我们希望用户在登录后被重定向到他在前一个会话中最后访问的页面。

实现这一目标的好方法是什么?

我在想一个httpmodule-->begin request or via the global.asax

我应该在请求过程中的哪个点放置逻辑来检查 cookie 是否存在并进行重定向?在Application.init?

任何建议将不胜感激!

4

2 回答 2

1

您可以创建一个自定义操作过滤器,将当前请求的 URL 保存到 cookie。然后检查登录操作方法中的 cookie 值,并在必要时重定向。

在这样做时,您可以仅装饰您想要的作为潜在入口点的控制器和操作。例如,不是返回部分视图等的操作。

于 2011-04-18T10:34:16.487 回答
0

没错,点击时没有事件。但是,有一个更简单的解决方案,MVC 可以很好地处理表单提交和重定向。要存储上次访问的 URL,您可以在控制器上使用操作过滤器。然后处理重定向,创建两个登录函数。一个处理 GET 请求,另一个处理 POST 请求。在 POST 请求中,验证身份验证后,从 cookie 中检索 URL(或操作)并重定向用户。

它会是这样的:

[HttpGet]
public ActionResult Login()
{
    return View();
}

[HttpPost]
public ActionResult Login(LoginViewModel model)
{
    if (authenticated)
    {
        //get cookie information
        HttpCookie cookie;
        cookie = Request.Cookies["StoredURLFromLastSession"];
        String StoredURLFromLastSession = cookie.Value;

        //Choose one of these redirect methods
        //returns to a hard coded URL
        //return Redirect(StoredURLFromLastSession);

        //redirects to a route (using routes created in global.asax
        //return RedirectToRoute(StoredURLFromLastSession);

        //redirects to a specific action/controller
        //return RedirectToAction(StoredURLFromLastSession);
    }
}

希望这可以帮助。

于 2011-04-18T10:42:03.743 回答