AspNetCore MVC - RedirectToAction return ignored

I have an action as shown 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 redirects to the EmailNotConfirmed action located in the same controller. But it is not redirected to the action of the HomeController Index . Instead, the browser remains on Account/SendEmailVerificationCode and displays a blank page.

HomeController.Index as follows:

 [HttpGet] public IActionResult Index() { return View(); } 

I tried:

  • At the beginning of SendEmailVerificationCode action was asynchronous, but HomeController.Index not. So I declared them as async.
  • Then I deleted the async declaration from both of them.
  • I tried return RedirectToAction("Index", "Home"); .
  • SendEmailVerificationCode has an HttpPost attribute; I changed it to HttpGet .

How can I redirect to an action in another controller?
Any help would be appreciated.

PS: I’ve been researching this problem for some time, and I read the solutions for issues such as:
MVC RedirectToAction is not working properly
RedirectToAction is ignored

But none of these questions or questions about the action being redirected after an ajax request helped me.

Thanks.

+5
source share
1 answer

I solved the problem by adding some entries to the application. It turns out that the actual problem was hiding.

I used TempData to store customized error messages, and I used it with the AddToErrorData function that I displayed in the question.

In AspNetCore, the Serializable attribute disappeared along with the ISerializable interface. Therefore, TempData was unable to serialize my custom list of IList objects.

When I changed TempData[ConstantParameters.ErrorData] = _errorData; on TempData[ConstantParameters.ErrorData] = JsonConvert.SerializeObject(_errorData); , the redirect problem has been resolved.

For reference: I also had to change the search string TempData as: _errorData = JsonConvert.DeserializeObject<ErrorDataList>(TempData[ConstantParameters.ErrorData].ToString());

+5
source

Source: https://habr.com/ru/post/1261837/


All Articles