[英]AspNetCore MVC - return RedirectToAction is getting ignored
I have an action as below: 我有一个动作如下:
[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 });
}
}
When I make the code jump into the else block (when debugging) it makes the redirection to EmailNotConfirmed
action, which is in the same controller. 当我将代码跳转到else块(调试时)时,它会重定向到
EmailNotConfirmed
操作,该操作位于同一个控制器中。 But it doesn't redirect to HomeController
's Index
action. 但它没有重定向到
HomeController
的Index
动作。 Instead, the browser stays at Account/SendEmailVerificationCode
and displays a blank page. 相反,浏览器停留在
Account/SendEmailVerificationCode
并显示空白页面。
HomeController.Index
is as below: HomeController.Index
如下:
[HttpGet]
public IActionResult Index()
{
return View();
}
I tried these: 我试过这些:
SendEmailVerificationCode
action was async but HomeController.Index
wasn't. SendEmailVerificationCode
操作是异步但HomeController.Index
不是。 So I declared them both as async. async
declaration from both of them. async
声明。 return RedirectToAction("Index", "Home");
return RedirectToAction("Index", "Home");
. SendEmailVerificationCode
had HttpPost
attribute; SendEmailVerificationCode
具有HttpPost
属性; I changed it to HttpGet
. HttpGet
。 How can I make the redirection to an action in a different Controller? 如何在不同的Controller中重定向到某个操作?
Any help would be appreciated. 任何帮助,将不胜感激。
PS: I have been doing research about this issue for a while now and I've read the solutions for questions such as: PS:我一直在研究这个问题,我已经阅读了以下问题的解决方案:
MVC RedirectToAction is not working properly MVC RedirectToAction无法正常工作
RedirectToAction gets ignored RedirectToAction被忽略
But none of these or the questions regarding the action not being redirected after an ajax request have helped me. 但是这些或者关于在ajax请求之后没有重定向的操作的问题都没有帮助我。
Thanks. 谢谢。
I figured the problem by adding some logging into the application. 我通过在应用程序中添加一些日志记录来解决问题。 It turns out that the actual problem was being hidden.
事实证明,实际问题正在被隐藏。
I was using TempData to store the customized error messages and I was utilizing it via the AddToErrorData
function I had displayed in the question. 我使用TempData存储自定义的错误消息,我通过问题中显示的
AddToErrorData
函数使用它。
In AspNetCore, Serializable
attribute has disappeared along with ISerializable
interface. 在AspNetCore中,
Serializable
属性与ISerializable
接口一起消失。 Therefore, TempData was unable to serialize my custom IList
object list. 因此,TempData无法序列化我的自定义
IList
对象列表。
When I changed the TempData[ConstantParameters.ErrorData] = _errorData;
当我更改
TempData[ConstantParameters.ErrorData] = _errorData;
to TempData[ConstantParameters.ErrorData] = JsonConvert.SerializeObject(_errorData);
to
TempData[ConstantParameters.ErrorData] = JsonConvert.SerializeObject(_errorData);
the redirection problem was solved. 重定向问题解决了。
For reference: I also had to change the TempData retrieving line as: _errorData = JsonConvert.DeserializeObject<ErrorDataList>(TempData[ConstantParameters.ErrorData].ToString());
供参考:我还必须将TempData检索行更改为:
_errorData = JsonConvert.DeserializeObject<ErrorDataList>(TempData[ConstantParameters.ErrorData].ToString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.