Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET Web API get child list (hierarchical resources)
    primarykey
    data
    text
    <p>I have the following rest schema that I'd like to implement using the ASP.NET Web Api:</p> <pre><code>http://mydomain/api/students http://mydomain/api/students/s123 http://mydomain/api/students/s123/classes http://mydomain/api/students/s123/classes/c456 </code></pre> <p>I've got the first two links working properly using the ApiController and the following two methods:</p> <pre><code>public class StudentsController : ApiController { // GET api/students public IEnumerable&lt;Student&gt; GetStudents() { } // GET api/students/5 public IEnumerable&lt;Student&gt; GetStudent(string id) { } } </code></pre> <p>In this same controller, (or do I need a different controller called ClassesController?), how would I implement the last two links? Also, what would the routing for the 'classes' part look like (if necessary)?</p> <p>Here's my WebApiConfig (which I'd like to keep as dynamic, rather than hard-coding the route to the /classes if possible:</p> <pre><code>config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); // EDIT - I'm getting 404's when trying to use this context.Routes.MapHttpRoute( name: "JobsApi", routeTemplate: this.AreaName + "/Students/{id}/Classes/{classId}", defaults: new { classId = RouteParameter.Optional } ); </code></pre> <p>EDIT Here's my newly created ClassesController: </p> <pre><code>public class ClassesController : ApiController { // GET api/classes public IEnumerable&lt;TheClass&gt; Get(string id) { return null; } } </code></pre> <p>I'm getting 404 Errors when attempting to go to this URL:</p> <pre><code>http://mydomain/api/students/s123/classes </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload