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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So why the first paramter addressId is passed correctly to addressId although parameter name is id in Default route?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.