WebAPI Route Attributes/No Attributes

Topics: ASP.NET Web API
Jul 10, 2014 at 3:57 PM
I have a controller that looks like the following:
public class FooController
        [HttpPost]//, Route("")]
        public string Post(string data) 
                return "post";

        [HttpPost, Route("bar")]
        public string Post2(string data)
                return "post2";
In WebApi 5.1.2 I could successfully make two requests, one to api/foo and api/foo/bar and would receive the results "post" and "post2" respectively.

I just upgraded to WebApi 5.2 and now I am receiving an Invalid Operation exception of "Multiple actions were found that match the request". This seems to be resolved if I add the commented blank/empty Route("") attribute to my "root" action.

Is this a bug? Or is this not supported. Please let me know, thanks!
Jul 10, 2014 at 4:22 PM

I tried your below controller(without commenting Route(“”)) with versions 5.0, 5.1.2 and 5.2 and the requests “api/foo” and “api/foo/bar” work just fine…also I made the “string data” to “[FromBody]string data” as that I believe that is what your intention is?...

Your repro code is not reflecting the problem that you are mentioning…can you fix it and give us an updated repro…

Just FYI…

Do you also happen to have a conventional route like “config.Routes.MapHttpRoute(“SomeRouteNameHere”, “api/{controller}/{id}” )” ?...please note that a request matching a conventional route wouldn’t know about any attributed controller/actions…



Jul 10, 2014 at 7:42 PM
Hey Kiran,

Looks like I left out some more detail. Months ago we wanted to introduce areas into our WebApi2 project and thus extended the DefaultHttpControllerSelector to overcome the hurdle of having two controllers with the same name but in different areas/namespaces and the ambiguous controller exception. In this case we create a new instance of an HttpControllerDescriptor using the constructor:
var descriptor = new HttpControllerDescriptor(_httpConfiguration, controllerName, areaControllerType);
After further inspection it seems that calling base.SelectController(request) creates an instance of HttpControllerDescriptor but has several other private members assigned that appear to help it resolve the proper action when code execution reaches the ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext) method, which then throws the "Multiple actions were found that match the request" exception.

This used to work in the previous WebApi 5.1.2 but not anymore in 5.2. Do you have any recommendations? I can attach a sample project if that would help. Thank you very much for your time in regards to this issue.