I have the following Route defined in Global.asax:
routes.MapRoute(
"IncidentActionWithId", // Route name
"Incidents/{companyId}/{action}/{id}", // URL with parameters
new { controller = "Incidents" } // Parameter defaults
);
I have a special case of request, like this one:
/Incidents/SuperCompany/SearchPeople/Ko
In this case, action should indeed map to SearchPeople
action, comapnyId
to this action's parameter, but only when action is SearchPeople, the Ko
should not be mapped to an id parameter of the action, but to searchTerm
.
My action declaration is:
[HttpGet]
public ActionResult SearchPeople(string companyId, string searchTerm)
How can I achieve Ko
to be mapped to searchTerm
parameter in my action method?
You can define two routes, one with id
and one with searchTerm
if the id is supposed to be numeric (or you can specify regex constratints) and have different pattern to searchTerm.
See here how you can define constraints.
Example:
routes.MapRoute(
"IncidentActionWithId", // Route name
"Incidents/{companyId}/{action}/{id}", // URL with parameters
new { controller = "Incidents" }, // Parameter defaults
new {id = @"\d+"} // numeric only
);
routes.MapRoute(
"IncidentActionWithId", // Route name
"Incidents/{companyId}/{action}/{searchterm}", // URL with parameters
new { controller = "Incidents" }
);
Define the one with constraint first.