Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't RedirectToAction with Parameter when using MapControllerRoute

While having this route mapping:

app.MapControllerRoute(
    "ViewAct",
    "Cont1/Act1/{id}/{title?}",
    new { controller = "Cont1", action = "Act", id = "" });

When calling RedirectToAction from a different controller action, the id parameter will arrive as zero 0;

So when calling redirect from here:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public ActionResult Act2(long id, long nid) // id = 123
{    
    return RedirectToAction("Act1", new { id = id, title = "" });
}

public async Task<IActionResult> Act1(long id, string title) // id == 0

id will be 0.

Redirecting from Act1 to Act1 works (id gets value), also if I delete the MapControllerRoute on top, it will also work.

>Solution :

Remove the id value from the defaults parameter:

app.MapControllerRoute(
    "ViewAct",
    "Home/Act1/{id}/{title?}",
    new { controller = "Home", action = "Act1"});

Providing the valid default value (which can be parsed into long) for id also works:

app.MapControllerRoute(
    "ViewAct",
    "Home/Act1/{id}/{title?}",
    new { controller = "Home", action = "Act1", id = "0" });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading