【发布时间】:2016-12-24 23:53:11
【问题描述】:
我有一个动作如下:
[HttpGet]
public IActionResult SendEmailVerificationCode(int userId)
{
SpaceUser user = userManager.FindByIdAsync(userId).Result;
bool taskComleted = SendEmailVerificationLink(userId).IsCompleted;
if (taskComleted)
{
AddToErrorData(InfoMessages.EmailVerificationLinkSent_Params, user.Email);
return RedirectToAction(nameof(HomeController.Index), "Home");
}
else
{
return RedirectToAction("EmailNotConfirmed", new { userId = user.Id });
}
}
当我让代码跳转到 else 块(调试时)时,它会重定向到同一控制器中的 EmailNotConfirmed 操作。但它不会重定向到HomeController 的Index 操作。相反,浏览器停留在Account/SendEmailVerificationCode 并显示一个空白页面。
HomeController.Index如下:
[HttpGet]
public IActionResult Index()
{
return View();
}
我试过这些:
- 一开始
SendEmailVerificationCode操作是异步的,但HomeController.Index不是。所以我将它们都声明为异步的。 - 然后我从他们两个中删除了
async声明。 - 我试过
return RedirectToAction("Index", "Home");。 -
SendEmailVerificationCode有HttpPost属性;我把它改成了HttpGet。
如何重定向到不同控制器中的操作?
任何帮助将不胜感激。
P.S.:我已经对这个问题进行了一段时间的研究,并且我已经阅读了以下问题的解决方案:
MVC RedirectToAction is not working properly
RedirectToAction gets ignored
但是这些或关于在 ajax 请求后未重定向操作的问题都没有帮助我。
谢谢。
【问题讨论】:
-
尝试在
Startup.cs中添加一条新路由为routes.MapRoute("MainIndex", "{controller}/{action}", new { controller = "Home", action = "Index" });并通过:return RedirectToRoute("MainIndex");进行重定向,但没有成功。
标签: c# asp.net-core-mvc