0

I have Get actions in my controllers which have two parameters. I call them from Razor views like this using JS functions:

function deleteAddress(addressId) {
    url = '@Url.Action("Delete", "Addresses",new { addressId = "3",actionRouteAfterActionFinish="SelectAddress"})';
    openLink(url);
}

And here is declaration of my action inside controller:

public async Task<ActionResult> Delete(int addressId,string actionRouteAfterActionFinish){
   ...
   ...
}

When I add below Attribute route it works well and both paramters are passed correctly from Razor view to Delete action:

[Route("Addresses/Delete/{addressId}/{actionRouteAfterActionFinish}")]

But when I try to use below RouteConfig it does not work and 2nd parameter is set null when method is get called from Razor view. I wonder how can I rewrite RouteConfig to manage actions with two parameters?

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//enabling attribute routing
routes.MapMvcAttributeRoutes();
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Intro", id = UrlParameter.Optional }
);
routes.MapRoute(
    name: "TwoParamterAction",
    url: "{controller}/{action}/{parameter1}/{paramter2}"
);

I reordered Default rule with TwoParamterAction rule but it did not help.

VSB
  • 9,825
  • 16
  • 72
  • 145

1 Answers1

1

It's because the argument names do not match. Arguments in the route url can have a different order than the method arguments.

Because of that, the names must match.

routes.MapRoute(
    name: "TwoParamterAction",
    url: "{controller}/{action}/{addressId}/{actionRouteAfterActionFinish}"
);

That should work, since the names match.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • So why the first paramter `addressId` is passed correctly to `addressId` although parameter name is `id` in `Default` route? – VSB May 04 '20 at 07:30