mehrandvd
mehrandvd

Reputation: 9116

ASP.NET MVC routing doesn't use method name to find the action and just uses the parameters

Consider an Api controller like this:

public class MyApiController
{
    [HttpGet]
    public IEnumerable<object> GetItems(int from, int count)
    {
        ...
    }

    [HttpGet]
    public IEnumerable<object> GetActiveItems(int from, int count)
    {
        ...
    }
}

If I call /MyApi/GetActiveItems/?from=0&count=20 then it's possible to route the action GetItems instead of GetActiveItems because of parameters similarity. If I change the parameters name, for example (int fromActive, int countActive) it works properly.

Why is that so? Why doesn't it use the action name to match with the method name?

Should I do something in the routing?

Upvotes: 0

Views: 485

Answers (1)

mehrandvd
mehrandvd

Reputation: 9116

It seems the problem was about a bad routing set somewhere other than its usual:

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

and after then there was the correct routing:

routes.MapHttpRoute(
                name: "DefaultProvider",
                routeTemplate: "api/{controller}/{action}"
            );

In this case, as I haven't used {action} in the first routing, the action name goes to the {id} and the routing tries to resolve action by its parameters.

Upvotes: 1

Related Questions