I have a routing problem and I can’t figure out how to solve it.
There’s a controller named “UsersController” and it contains 2 Post action functions:
public int PostFBLogin(User userObject){}
public void PostUpdateImgUrl(User userObject){}
When I make a POST request, I pass a JSON representation of “User” in the request body.
If I comment out one of these functions, then it works fine.
But when the both functions exist, whenever I try to make a request to each one of them, I get the following error:
"Multiple actions were found that match the request:
Int32 PostFBLogin(MestoryServer.Models.User) on type MestoryServer.Controllers.UsersController
Void PostUpdateImgUrl(MestoryServer.Models.User) on type MestoryServer.Controllers.UsersController"
I tried to solve it by putting the following routes in the routing tables:
RouteTable.Routes.MapHttpRoute(
name: "UserPostUpdateImgUrlAction",
routeTemplate: "api/users/PostUpdateImgUrl/",
defaults: new
{
controller = "users",
action = "PostUpdateImgUrl"
}
);
RouteTable.Routes.MapHttpRoute(
name: "UserPostFBLoginAction",
routeTemplate: "api/users/PostFBLogin/",
defaults: new
{
controller = "users",
action = "PostFBLogin"
}
);
But it didn’t help.
After looking at lots of posts about routing tables on the internet, I’m not sure that it’s even possible to have two actions which have the same signatures but different names.
Can anyone help?
Thanks,
Edi.
When you make a HTTP request with the Web API, the HTTP method is used to identity the action to invoke. A HTTP Post request will invoke the method within the ApiController that starts with “POST”. By convention the Web API expects (at most) one method per HTTP method. This is the cause of the error you are receiving.
The default route for Web API does not specify action:
You could alter the above to include action, e.g.
routeTemplate: "api/{controller}/{action}/{id}"but then you have to add[HttpPost]above the methods.Or you could consider splitting your Web Api controller into two. The PostFBLogin method could be moved into a LoginController and the PostUpdateImgUrl method could stay in the UsersController.