Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I map multiple Request DTOs to a single Route
    primarykey
    data
    text
    <p>I have been experimenting with the <a href="https://github.com/ServiceStack/ServiceStack" rel="nofollow">ServiceStack</a> framework to develop a simple REST API. I am having troubles trying to define the Routes properly to handle the different URLs I desire.</p> <p>I have a simple DomainModel class:</p> <pre><code>public class Student { public int Id { get; set; } // unique public string Email { get; set; } // not unique // some other properties and business logic } </code></pre> <p>I would like to create a REST service which responds to HTTP requests to the following URLs:</p> <ul> <li><code>/students</code> <ul> <li>GET returns all students</li> </ul></li> <li><code>/students/123</code> <ul> <li>GET returns student with Id = 123</li> </ul></li> <li><code>/students?id=123</code> <ul> <li>GET returns student with Id = 123</li> </ul></li> <li><code>/students?email=foo@example.com</code> <ul> <li>GET returns all students with matching Email</li> </ul></li> </ul> <p>I have tried using the following Request DTOs to accomplish this:</p> <pre><code>[Route("/students", "GET")] public class GetAllStudents : IReturn&lt;StudentList&gt; { } [Route("/students", "GET")] // I want this to be for /students?Id= [Route("/students/{Id}", "GET")] public class GetStudentById : IReturn&lt;Student&gt; { public int Id { get; set; } } [Route("/students", "GET")] // I want this to be for /students?Email= public class GetStudentsByEmail : IReturn&lt;StudentList&gt; { public string Email { get; set; } } </code></pre> <p>As you can see, I am only trying to use two different Response DTOs:</p> <pre><code>public class StudentList : List&lt;Student&gt; { } public class Student { public int Id { get; set; } public String Email { get; set; } } </code></pre> <p><strong>Note:</strong> This Reponse DTO <code>Student</code> class is separate than my DomainModel class; they are in different namespaces.</p> <p>Unfortunately, when I try to use these routes, every request to <code>/students?&lt;anything&gt;</code> ends up returning all students. The only Route that does work is if I make a GET request to <code>/students/123</code>, then I get back Student with Id 123. I assume this isn't working because I am re-using the <code>"/students"</code> Route definition for multiple Request DTOs.</p> <p>I have been reading through the ServiceStack <a href="https://github.com/ServiceStack/ServiceStack/wiki" rel="nofollow">wiki</a> and <a href="https://github.com/ServiceStack/ServiceStack.Examples" rel="nofollow">examples</a> but I can not figure out how to accomplish what I am attempting. The most detailed documentation I can find about advanced routing is the <a href="https://github.com/ServiceStack/ServiceStack/wiki/New-API#smart-routing" rel="nofollow">"Smart Routing" section of the "New API" page</a> but it doesn't really address this situation either.</p> <p>How can I re-use the same Route definition for mulitple Request DTOs?</p>
    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.
    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