简体   繁体   English

AspNetCore MVC - 返回RedirectToAction被忽略了

[英]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. 但它没有重定向到HomeControllerIndex动作。 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: 我试过这些:

  • In the beginning SendEmailVerificationCode action was async but HomeController.Index wasn't. 在开始时SendEmailVerificationCode操作是异步但HomeController.Index不是。 So I declared them both as async. 所以我宣称它们都是异步的。
  • Then I deleted the async declaration from both of them. 然后我从它们中删除了async声明。
  • I tried 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM