2

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.

1 Answer 1

1

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:

routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

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.

Sign up to request clarification or add additional context in comments.

3 Comments

So now I tried to use the following route: RouteTable.Routes.MapHttpRoute(name: "ActionApi",routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); and I also put [HttpPost] on both functions. But I still get the same error.
That should work. It could be caused by a route conflict. Did you add the new route instead of changing the default? In that case, the route with action needs to be specied before the route without action.
Great! Now it works! Actually in Application_Start there wasn't any DefaultApi routing. It was already inside the routing list. So I had to insert the new route before the DefaultApi route inside the list - like you said.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.