2013-05-12 124 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