Can't RedirectToAction with Parameter when using MapControllerRoute

150 views Asked by At

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:

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.

1

There are 1 answers

2
Guru Stron On BEST ANSWER

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" });