ASP.NET Web API gets child list (hierarchical resources)

I have the following remainder scheme that I would like to implement using ASP.NET Web Api:

http://mydomain/api/students
http://mydomain/api/students/s123
http://mydomain/api/students/s123/classes
http://mydomain/api/students/s123/classes/c456

My first two links work correctly using ApiController and the following two methods:

public class StudentsController : ApiController {
  // GET api/students
  public IEnumerable<Student> GetStudents() {  
  }

  // GET api/students/5
  public IEnumerable<Student> GetStudent(string id) {  
  }
}

In the same controller (or do I need another controller called ClassesController?), How would I implement the last two links? In addition, what does routing look like for some classes (if necessary)?

Here is my WebApiConfig (which I would like to save as a dynamic rather than hard-coding route to / classes, if possible:

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

// EDIT - I'm getting 404 when trying to use this
context.Routes.MapHttpRoute(
  name: "JobsApi",
  routeTemplate: this.AreaName + "/Students/{id}/Classes/{classId}",
  defaults: new { classId = RouteParameter.Optional }
);   

EDIT Here is my newly created ClassesController:

public class ClassesController : ApiController {
  // GET api/classes
  public IEnumerable<TheClass> Get(string id) {    
      return null;
  }
}

I get 404 errors when trying to go to this url:

http://mydomain/api/students/s123/classes
+5
source share
2

, . , : PingYourPackage. .

. , , . :

, . , . , -:

  • Affiliate1 (Id: 100)
  • Affiliate2 (Id: 101)

, , :

  • Affiliate1 (: 100)
    • 1 (: 100)
    • 2 (: 102)
    • 4 (: 104)
  • Affiliate2 (: 101)
    • 3 (: 103)
    • 5 (: 105)

, :

GET api/affiliates/{key}/shipments
GET api/affiliates/{key}/shipments/{shipmentKey}
POST api/affiliates/{key}/shipments
PUT api/affiliates/{key}/shipments/{shipmentKey}
DELETE api/affiliates/{key}/shipments/{shipmentKey}

@ , . , GET /api/affiliates/105/shipments/102. , - 105, . , . .

, ( ), . , , Affiliate1 , AuthorizeAttribute, " ". , , Affiliate1 : /api/affiliates/101/shipments, Affiliate2. AuthorizeAttribute.

, URI :

GET/api/affiliates/100//102

, URI:

GET/api/affiliates/100//103

HTTP "404 Not Found", , 100 , 103.

+7

ASP.NET , . , 2 :

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

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

:

public class ClassesController
{
   public TheClass Get(int studentId, int classId)
   {
       ....
   }
}

, , .

, - -API, , .

+6