Action 會依照HTTP Method的名稱的開頭及撘配參數來做選擇
而 HTTP method 有 "Get", "Post", "Put", "Delete", "Head", "Options", or "Patch" 這幾種
例如
註冊的Route是
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Controller 的類別裡面有底下2個method
public IEnumerable<Product> GetOOO()
public Product GetXXX(int id)
當URL 為
/api/product 會呼叫 public IEnumerable<Product> GetOOO()
因為上面的url 沒有參數所以會選一個get開頭且沒有參數的method
/api/product/1 會呼叫 public Product GetXXX(int id)
因為上面的url 有參數所以會選一個get開頭且參數為id的method
變化型:
Controller 的類別裡面再增加2個method
public IEnumerable<Product> Get(string name)
public IEnumerable<Product> GetProductsByCategory(string category)
當URL為
/api/product?name=ooo 會呼叫 public IEnumerable<Product> Get(string name) 因為有指定參數名稱為name
/api/product?category=ooo 會呼叫 public IEnumerable<Product> GetProductsByCategory(string category) 同上,因為有指定參數名稱為category
變化型2:
如果controllerl裡有多個參數的method
public IEnumerable<Product> Get(int id, string name)
則使用/api/product?id=ooo&name=xxx 即可
詳細參考資料
http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection