2013-05-12 59 views
0

我收到以下錯誤'非空類型的「身份證失敗:寧靜的API獲取有關「參數字典包含參數無效項」

參數字典包含參數無效項」身份證'在'RecipeTracker.Controllers.StandardDirectionsController'中用於方法'System.String Get(Int32)'的非空類型'System.Int32'。可選參數必須是引用類型,可爲空類型,或者聲明爲可選參數。

在我的全局文件我有這樣的定義:

protected void Application_Start(object sender, EventArgs e) 
    { 
     // Code that runs on application startup 
     RouteTable.Routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { id = System.Web.Http.RouteParameter.Optional }); 
    } 

這是我的控制器:

App_Data.databaseDataContext _context = new App_Data.databaseDataContext(); 
    // GET api/<controller> 
    public List<string> Get() 
    { 
     var direction = (from d in _context.ViewStandardDirections("-1") 
         select d.Direction); 

     return direction.ToList(); 
    } 

    // GET api/<controller>/5 
    public List<Models.DirectionChoices> Get([FromUri]string q) 
    { 
     var choices = (from i in _context.ViewStandardDirections(q) 
         select new Models.DirectionChoices 
         { 
          text = i.Direction 
         }); 
     return choices.ToList(); 
    } 

這是我嘗試和失敗的網址:

http://localhost:9328/api/standarddirections/heat 

如果我刪除/ heat部分,然後它查詢數據庫就好了。我根據其他帖子的建議添加了[FromUri],但它的行爲與沒有它的行爲相同。

回答

5

參數名稱必須匹配的路由:

//public List<Models.DirectionChoices> Get([FromUri]string q) 
    public List<Models.DirectionChoices> Get([FromUri]string id) 
+0

啊,是我現在的固定!謝謝 :) – Yecats 2013-05-12 21:04:21