Reputation: 2577
can I have domain.com/action/id as well as domain.com/controller/action?
how would I register these in the route-table?
Upvotes: 1
Views: 105
Reputation: 181004
Is ID always guaranteed to be a number? If yes, then you could use RouteConstraints:
routes.MapRoute("ActionIDRoute",
"{action}/{id}",
new { controller = "SomeController" },
new {id= new IDConstraint()});
routes.MapRoute("ControllerActionRoute",
"{controller}/{action}",
new {}); // not sure about this last line
The IDConstraint class looks like this:
public class IDConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route,
string parameterName, RouteValueDictionary values,
RouteDirection routeDirection)
{
var value = values[parameterName] as string;
int ID;
return int.TryParse(value,out ID);
}
}
Basically what is happening is that you have two identical routes here - two parameters, so it's ambigous. Route Constraints are applied to parameters to see if they match.
So:
If ID is not guaranteed to be a number, then you have the problem to resolve the ambiguity. The only solution I am aware of is hardcoding the routes where {action}/{id} applies, which may not be possible always.
Upvotes: 2
Reputation: 16926
Yes, you can add a new rule above the default rule and provide a default value for the controller.
routes.MapRoute(
"MyRole", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
The sample routs all actions to the "Home" controller.
Upvotes: 1