Similar route mapping

Posted by Denis Agarev on Stack Overflow See other posts from Stack Overflow or by Denis Agarev
Published on 2012-06-26T13:34:29Z Indexed on 2012/06/26 15:16 UTC
Read the original article Hit count: 263

I need to map to create controller what must response to two urls:

  1. "http://localhost/api/controller?id=1" (where only id value can change)
  2. "http://localhost/api/controller/anotherId/someconst" (where anotherId is the only one changing part)

i map it to such route:

routes.MapHttpRoute("Test", "api/{controller}/{id}/{someconst}", new { controller = "Test", someconst = RouteParameter.Optional });

And have to methods in my controller:

public void Get(int id) { ... }
public void Get(int anotherId, string someconst ) { ... }

It works...But it doesn't look nice...cause "string someconst" is not a param, it's just a const part of url. But if i remove "string someconst" param second url wouldn't work. Is it possible to map one controller to two routes to resolve this urls to make it clear without fake param which is a const in fact?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about asp.net-mvc-routing