I am using RouteAttributes in my projects, and they are working correctly, except in this case: I have two similar actions, one accepting two int parameters, and the other accepting just one.
The first one takes the two parameters and uses them to load an object and pass it to the View; The second one just tries to find a default value for the second variable, and redirects to the first action, passing the two variables.
The problem is that in ActionResultExample(int idVar1)
, I expect to be redirected to ControllerExample/Edit/idVar1/Brands/idVar2
; however I am redirected to ControllerExample/Edit/idVar1/Brands?idVar2=idVar2
.
This generates an infinite redirection loop, of course.
Here is the example code:
[Route("ControllerExample/Edit/{idVar1}/Brands/{idVar2}")]
public ActionResult ActionResultExample(int idVar1, int idVar2)
{
var objectForView= ObjectController.Get(idVar1, idVar2);
return View("ActionResultExample", objectForView);
}
[Route("ControllerExample/Edit/{idVar1}/Brands")]
public ActionResult ActionResultExample(int idVar1)
{
...
var idVar2 = getDefaultVar2();
return RedirectToAction("ActionResultExample", //redirects to ControllerExample/Edit/idVar1/Brands?idVar2=idVar2 instead of ControllerExample/Edit/idVar1/Brands/idVar2
new {idVar1= idVar1, idVar2 = idVar2 });
}