【问题标题】:Redirecting action to another controller within the same area将动作重定向到同一区域内的另一个控制器
【发布时间】:2013-11-29 18:21:21
【问题描述】:

我正在尝试让我的网站在成功登录时运行重定向。我将所有会员路线都保存在一个区域中。

/membership/login/index
/membership/profile/index

我已经尝试了以下两种方法来尝试使其正常工作 - 登录成功,但重定向除了更改地址之外没有任何作用。我尝试了以下方法,但结果都一样

[HttpPost]
public ActionResult Index(Login loginViewModel)
{
    ...
    return RedirectToRoute(new { Area = "Membership", 
               Controller = "Profile", Action="Index" });
}

结果

http://mysite.com/Membership/Login?ReturnUrl=%2fmembership%2fProfile

那么我的下一个努力

return RedirectToAction("Index", "Profile", new { Area = "Membership" });
....
http://mysite.com/Membership/Login?ReturnUrl=%2fmembership%2fProfile

最后

return RedirectToAction("Index", "Profile");
... 
http://mysite.com/Membership/Login?ReturnUrl=%2fmembership%2fProfile

【问题讨论】:

  • HTTP POST 无法重定向。但除了将其更改为 GET 之外,我不知道解决方法。如果你把它改成HttpGet,会发生什么?
  • 谢谢,很高兴知道,我不太了解http的来龙去脉
  • 您的登录好像没有成功。您总是重定向回再次登录。你能显示你的 ActionResut 的完整代码吗?
  • 我已经调试过了,示例中的代码肯定在执行中

标签: asp.net-mvc-4 asp.net-mvc-routing


【解决方案1】:

您当然可以重定向 HTTP 帖子(您不能做的是重定向它,然后期望重定向的响应也是 POST)。这里发生的情况是您成功地将请求重定向到正确的页面,但是有问题的页面需要身份验证,并且由于用户未通过身份验证,因此请求将再次发送回登录页面。您应该在设置 cookie 之后进行重定向(或执行您正在执行的任何身份验证机制)。

【讨论】:

  • 非常有道理,早上试一试
  • 确实是这样,认证不正常
【解决方案2】:

您必须在重定向之前保存 cookie..

FormsAuthentication.SetAuthCookie("UserName or somthing",false/true);
return RedirectToAction("Index", "Profile", new { Area = "Membership" });

现在它可以工作了..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-30
    • 2018-04-19
    • 1970-01-01
    • 2011-06-17
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    相关资源
    最近更新 更多