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. But it doesn't redirect to HomeController
's Index
action. Instead, the browser stays at Account/SendEmailVerificationCode
and displays a blank page.
HomeController.Index
is as below:
[HttpGet]
public IActionResult Index()
{
return View();
}
I tried these:
SendEmailVerificationCode
action was async but HomeController.Index
wasn't. So I declared them both as async.async
declaration from both of them.return RedirectToAction("Index", "Home");
.SendEmailVerificationCode
had HttpPost
attribute; I changed it to HttpGet
.How can I make the redirection to an action in a different Controller?
Any help would be appreciated.
P.S.: I have been doing research about this issue for a while now and I've read the solutions for questions such as:
MVC RedirectToAction is not working properly
RedirectToAction gets ignored
But none of these or the questions regarding the action not being redirected after an ajax request have helped me.
Thanks.
return RedirectToAction()To redirect to a different action which can be in the same or different controller. It tells ASP.NET MVC to respond with a browser to a different action instead of rendering HTML as View() method does. Browser receives this notification to redirect and makes a new request for the new action.
RedirectToAction will return a http 302 response to the browser and then browser will make GET request to specified action. Show activity on this post. Ideally I would use RedirectToRoute for Action Links/Images and RedirectToAction in Controller's Action to redirect to another Controller's Action .
To pass multiple values to the new controller method, set TempData values and/or pass them as parameters. First, add keyword/value pairs to the TempData collection to pass any number of values to the view. The temp data collection is persisted across controller method calls.
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.
In AspNetCore, Serializable
attribute has disappeared along with ISerializable
interface. Therefore, TempData was unable to serialize my custom IList
object list.
When I changed the TempData[ConstantParameters.ErrorData] = _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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With