3

I have a VideoController and inside of it there are 2 methods like the following:

[Route("api/Video/{id:int}")] public Video GetVideoByID(int id){ do something} [Route("api/Video/{id}")] public Video GetVideoByTitle(string id) {do something} 

The WebApiConfig.cs is like the following:

public const string DEFAULT_ROUTE_NAME = "MyDefaultRoute"; public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: DEFAULT_ROUTE_NAME, routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } 

So, when I comment out any of the method, the other one works, like if you totally comment out the 1st one, the 2nd method works, but both doesn't work when implemented. I used an Empty Web API template.

So any thoughts regarding why this is happening would be great.

3
  • can you try something like "id_str" instead of "id" in GetVideoByTitle and let me know? Commented Oct 2, 2014 at 7:37
  • I don't think this will work with only different names for the parameters, even changing the number of parameters won't help. Is it impossible to change the route to differentiate between the two. Maybe look into ActionAttributes to mark one as different. Commented Oct 2, 2014 at 7:46
  • @user3036757 Do you in effect want multiple submit buttons on one 'page' each going to a different action? Commented Oct 2, 2014 at 8:04

2 Answers 2

4

You have to enable the attribute routing calling MapHttpAttributeRoutes during configuration.

Example:

public static void Register(HttpConfiguration config) { // Web API routes config.MapHttpAttributeRoutes(); ... } 

I've tested it and worked for me correctly:

http://localhostapi/Video/1 // goes for the first method http://localhostapi/Video/foo // goes for the second method 
Sign up to request clarification or add additional context in comments.

Comments

1

Change your routeTemplate from

routeTemplate: "api/{controller}/{id}", 

to

routeTemplate: "api/{controller}/{action}/{id}", 

So you'd call something like api/Video/GetVideoByTitle/x or api/Video/GetVideoByID/x

You may want to read this, under Routing Variations > Routing by Action Name

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.