datatest
datatest

Reputation: 473

Routing, hitting wrong action

I have two actions, and whenever I explicitly call the second one, the first one still gets called. I need to be able to write in mysite.com/api/Awesome/GetSomeExamples, without it always triggering GetAllClasses

public class AwesomeController : Controller
{
   public IEnumerable<myClass> GetAllClasses()
   {
      //...some stuff
   }
   public IEnumerable<string> GetSomeExamples()
   {
       //...some stuff
   }
     //... also have some more actions which take in one parameter
}

my routing is as such, but not working

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "ActionApi",
                url: "api/{controller}/{action}/{id}",
                defaults: new { id = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

Error I am getting: Multiple actions were found that match the request

Upvotes: 2

Views: 1409

Answers (1)

Kenneth
Kenneth

Reputation: 28737

You are using Web API.

The difference with normal MVC actions is that Web API looks at the HTTP verb to determine the action to call. In this case you have two methods that start with Get

However, this should trigger an error: Multiple actions were found that match the request... and it shouldn't call both actions.

The URL you need is:

mysite.com/api/Awesome/

From there, Web API will call a method that starts with Get. You need to remove the other get method and put it in a different controller. Web API supposes you have one action per http verb

Upvotes: 5

Related Questions