2016-01-07 36 views
0

我正在尝试使用ActionName方法创建带有多个Get命令的WebAPI控制器。我在另一个项目上成功完成了这项工作,但是这个最新项目一直存在问题,无法理解为什么我的挖空视图模型ajax调用无法找到特定的URI。ASP.Net WebAPI ActionName Route Not Found

WebApiConfig.cs:

config.Routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{action}/{id}", 
      defaults: new { id = RouteParameter.Optional } 
     ); 

控制器:

// GET api/lot 
    [ActionName("Default")] 
    public IEnumerable<DataObject> Get() 
    { 
     //... 
    } 

    // GET api/lot/Specific/5 
    [ActionName("Specific")] 
    public IEnumerable<DataObject> Get(int? data) 
    { 
     //... 
    } 

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

为得到我的默认操作完美的作品,但在具体操作继续,当我试图从视图 - 调用它有这个错误型号:

“无法加载资源:服务器响应状态为404(未找到)”

我添加旁边[HTTPGET] [ActionName( “专用”)]并具有如下错误:

“GET http://localhost:57492/api/lot/Specific/1 404(未找到)”

我已经尝试了几种不同的东西,如删除int?数据参数,但是当我尝试构建项目时,它告诉我已经存在具有相同参数的现有函数,即使使用不同的操作名称。

最终我想要有多个Get(参数)操作来调用与我的视图模型进行交互。

回答

1

我能够通过HTTPGET添加路由属性和重命名我的所有功能,像GETALL(),GetSpecific(IND ID)等唯一的名称来解决这个..

// GET api/lot 
    [HttpGet] 
    [Route("api/lot/GetAll")] 
    public IEnumerable<DataObject> GetAll() 
    {...} 

    // GET api/lot/GetSpecific/{id} 
    [HttpGet] 
    [Route("api/lot/GetSpecific/{id}")] 
    public IEnumerable<DataObject> GetSpecific(string id) 
    { 
0

添加新在config.Routes中路由如下。这会有帮助吗?我没有测试这个。

config.Routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/specific/{data}", 
      defaults: new { 
       action = "Specific", 
       data = RouteParameter.Optional } 
     );