路由,打错了动作

dat*_*est 2 asp.net-mvc url-routing asp.net-mvc-routing asp.net-mvc-4

我有两个动作,每当我明确调用第二个动作时,第一个动作仍会被调用.我需要能够在mysite.com/api/Awesome/GetSomeExamples中编写,而不会总是触发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
}
Run Code Online (Sandbox Code Playgroud)

我的路由是这样的,但不起作用

 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 }
            );
        }
Run Code Online (Sandbox Code Playgroud)

我收到错误:找到了与请求匹配的多个操作

Ken*_*eth 5

您正在使用Web API.

与普通MVC操作的不同之处在于Web API查看HTTP谓词以确定要调用的操作.在这种情况下,您有两种方法Get

但是,这应该会触发错误:Multiple actions were found that match the request... 它不应该同时调用这两个操作.

您需要的URL是:

mysite.com/api/Awesome/
Run Code Online (Sandbox Code Playgroud)

从那里,Web API将调用以...开头的方法Get.您需要删除其他get方法并将其放在不同的控制器中.Web API假设您每个http动词有一个动作