Question

I am building a Http service using asp.net web api, I have two get methods in the controller with the same parameters, I can't figure out how to define the route the matches both methods, I can call only one of them and for the other I get an error that no action method was found on the controller. Here are the routes defined

RouteTable.Routes.MapHttpRoute(
         name: "default",
         routeTemplate: "{controller}/{lang}",
         defaults: new { lang = System.Web.Http.RouteParameter.Optional });
RouteTable.Routes.MapHttpRoute(
         name: "details",
         routeTemplate: "{controller}/{lang}/{action}/{id}");

and the methods in the controller:

public IQueryable<RecipeDTO> Get(string lang)
{

}

[HttpGet]
public RecipeDTO Details(string lang, int id)
{

}

[HttpGet]
public IQueryable<RecipeDTO> Random(string lang, int count)
{


}

You see the methods Details and Random have the same parameters, I can make the following calls:

controller-name/en (which matches the first get method)

controller-name/en/details/1 (which matches the details method)

but when I try:

controller-name/en/random/5

I get the error no action method was found on the controller, how can I fix that.

Thanks in advance

Was it helpful?

Solution

Web API is strict about matching route variables with parameter names. You can try changing your action to the following(Here we are aliasing the count parameter):

[HttpGet]
public IQueryable<RecipeDTO> Random(string lang, [FromUri(Name = "id")]int count)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top