2012-05-16 2 views
0

내가 내 전체 데이터베이스에 대한 컨트롤러가, 코드는 다음과 같습니다 : 당신은 내가 하나의 컨트롤러에서 여러 된 IQueryable을 볼 수 있듯이, 각각 다른 테이블을 쿼리하나의 컨트롤러에 여러 IQueryable이 있습니까?

public class YogaController : DbDataController<Yoga.Models.YOGAEntities> 
{ 
    public YogaController() 
    { 
    } 

    public IQueryable<Yoga.Models.Action> GetActions(int BugId) 
//GetActions retrieves "actions" table from the db, not Actions in MVC term 
    { 
     return DbContext.Actions.Where(x => x.FK_BugsID == BugId); 
    } 
    public IQueryable<Yoga.Models.Label> GetRequiredLabels() 
    { 
     return DbContext.Labels.Where(x => x.IsRequired == true); 
    } 
    public IQueryable<Yoga.Models.Role> GetRoles() 
    { 
     return DbContext.Roles; 
    } 
    public IQueryable<Role> GetRoles2() //TODO: finish this 
    { 
     return DbContext.Roles.Where(x => x.RoleID == 1); 
    } 
    public IQueryable<Tag> GetTags(int actionid) 
    { 
     return DbContext.Tags.Where(x => x.J_Tags.Any(y => y.FK_ActionID == actionid)); 
    } 
} 

. 그것은 금지 된 것입니까? 내가 localhost/api/Yoga/GetActions 또는 localhost/api/Yoga/GetRequiredLabels에 갈 때 때문에 나는 오류 메시지 :

Multiple actions were found that match the request: 
System.Linq.IQueryable`1[Yoga.Models.Label] GetRequiredLabels() on type Yoga.Controllers.YogaController 
System.Linq.IQueryable`1[Yoga.Models.Role] GetRoles() on type Yoga.Controllers.YogaController 
System.Linq.IQueryable`1[Yoga.Models.Role] GetRoles2() on type Yoga.Controllers.YogaController 

내가 하나 된 IQueryable하지만 모두 해제

, 결과가 잘 나왔다.

나는 비슷한 문제에 대한 봤 거든 내 라우팅 설정을 확인, 거기에 컨트롤러 경로와 이름에 충돌이 있습니다.

내 경로 (기본값 생성) :

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
     routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { id = RouteParameter.Optional } 
     ); 
     //routes.MapRoute(
     // name: "Default", 
     // url: "{controller}/{action}/{id}", 
     // defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 

어떤 아이디어?

+2

조치 방법의 반환 유형으로 IQueryable을 반환하는 이유는 무엇입니까? – Shyju

+1

MVC4를 사용하여 본 튜토리얼입니다. http://goo.gl/mqzDD 프론트 엔드가 upshot.js를 사용하고 IQueryable을 직접 처리하는 곳입니다. 정확한 메커니즘을 잘 모르겠습니다. – Bonk

+1

IQueryable을 반환하면 해당 작업의 쿼리 문자열에 OData 필터와 옵션을 사용할 수 있습니다. – diaho

답변

3

MVC4는 'Get'으로 시작하고 매개 변수가없는 모든 메소드에 대해 HTTP 동사 (Get)와 일치하는 것으로 보입니다. 작업 이름을 강제로 시도해보십시오

[ActionName("GetRequiredLabels")] 
public IQueryable<Yoga.Models.Label> GetRequiredLabels() 
... 
[ActionName("GetActions")] 
public IQueryable<Yoga.Models.Action> GetActions(int BugId) 
... // etc 

편집 : 붙여 넣은 경로와 컨트롤러를 기반으로

, 당신의 경로가 있어야한다고 생각 :

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    routes.MapHttpRoute(
     name: "DefaultApi", 
     routeTemplate: "api/{controller}/{action}/{id}", 
     defaults: new { id = RouteParameter.Optional } 
    ); 
} 

그것은이 있어야합니다 즉 {action}이 있습니다. 'Get'메서드가 하나만있는 경우 기본 MVC4 경로가 작동합니다. 여러 명이 있으니 경로에 따라 액션을 선택하도록 강요해야합니다.

+0

그냥 해봤는데, 나는 같은 오류가 발생합니다. 'localhost/api/Yoga/GetActions? BugId = 1' 매개 변수를 포함 시키려고 시도했지만 단순히 '요가'컨트롤러와 일치하는 동작이 없습니다. ' – Bonk

+1

확실히 그렇습니다 동일한 작업을 수행하는 컨트롤러가 있으므로 IQueryable을 반환하는 여러 액션이 있기 때문에가 아닙니다. 당신은 당신의 노선을 게시 할 수 있습니까? – diaho

+0

감사! 방금 내 경로를 포함하도록 업데이트되었습니다. – Bonk

관련 문제