I agree that designing URLs in this fashion isn't good, but doing this is entirely possible with some route magic and constraints.
The first route rule catches URLs with strings that match the regex which looks for numbers. The second rule takes effect if the first one doesn't.
routes.MapHttpRoute(
name: "ProductsById",
routeTemplate: "products/{id}",
defaults: new { controller = "Products", action = "GetProductById" },
constraints: new { id = @"\d+" }
);
routes.MapHttpRoute(
name: "ProductsByName",
routeTemplate: "products/{name}",
defaults: new { controller = "Products", action = "GetProductByName" }
);
SiggiGG
Member
265 Points
105 Posts
Re: How many number of GETs, POSTs allowed ?
Feb 18, 2012 09:50 AM|LINK
I agree that designing URLs in this fashion isn't good, but doing this is entirely possible with some route magic and constraints.
The first route rule catches URLs with strings that match the regex which looks for numbers. The second rule takes effect if the first one doesn't.
routes.MapHttpRoute( name: "ProductsById", routeTemplate: "products/{id}", defaults: new { controller = "Products", action = "GetProductById" }, constraints: new { id = @"\d+" } ); routes.MapHttpRoute( name: "ProductsByName", routeTemplate: "products/{name}", defaults: new { controller = "Products", action = "GetProductByName" } );