2013-11-03 64 views
3

我WebApiConfig.cs路由古怪

行爲

我的控制文件是:

namespace myApp.Data 
{ 
    public class AccountController : ApiController 
    { 
    public List<User> XGetUsers() 
    { 
     List<User> Users; 
     using (Entities db = new Entities()) 
     { 
      var users = from u in db.Users select u; 
      Users = users.ToList(); 
     } 
     return Users; 
    } 

    // GET api/<controller>/5 
    public string Get(int id) 
    { 
     return "value"; 
    } 
    } 
} 

但每當我試着撥打本地主機:51255/API /帳號/ XGetUsers我不斷收到一個400錯誤:

{"Message":"The request is invalid.","MessageDetail":"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String Get(Int32)' in myApp.Data.AccountController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."}

看來,所有的調用都會去Get(int id)方法。哪裏不對?

回答

4

您的路線缺少行動:

routeTemplate: "api/{controller}/{action}/{id}", 

因此它被認爲是{id}XGetUsers

+0

@OldGeezer,不用擔心,我很高興我可以幫助! –

1

您似乎只定義了一種可能性:控制器之後的部分是一個id。只有一個匹配的動作,因此該動作是唯一被調用的候選者。

也許你還想添加一個"api/{controller}/{action}/{id}"來匹配你的其他候選人。