This is supposed right:
/api/MyDataController.cs
public class MyDataController: ApiController
{
[HttpGet]
[Route("GetOne")]
public IHttpActionResult GetOne() { } // works w/o GetTwo
[HttpGet]
[Route("GetTwo")]
public IHttpActionResult GetTwo() { }
}
.js
$http({method: 'GET', url: '/api/MyData/GetOne'})... //works w/o GetTwo
$http({method: 'GET', url: '/api/MyData/GetTwo'})...
Same as this post, API version is
<package id="Microsoft.AspNet.WebApi" version="5.2.3"
targetFramework="net461" />
Both call to One and Two complained about GetOne,
"Multiple actions were found that match the request: GetOne on type MyWeb.API.MyDataControllerGetOne on type MyWeb.API.MyDataController"
It works if rem-out GetTwo() from Api controller.
This looks like the application is still using convention-based routing.
The reason for the clash is because the default convention-based route template
api/{controller}/{id}
does not usually provide an action parameter like thisapi/{controller}/{action}/{id}
. If you want to get post actions to work via convention-routing when use the template provided before.If you want to use attribute routing instead then you need to enable attribute routing in the
WebApiConfig.cs
file in order to allow theRout
Atrribute to work.You would also need to update your routes to get what you want
Readup on attribute routing here Attribute Routing in ASP.NET Web API 2
This is additional answer for legacy app developers like me:
After digging around, this is the best found. If you just have Global.asax without .cs, simply add it into .asax.