Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Doing a REST styled API I would like to know how I deal with duplicate action names</p> </blockquote> <p>In a RESTful styled API you should never have to deal with such duplicates. In a RESTful styled API you are dealing with resources.</p> <p>So in your particular case you have 2 resources:</p> <ul> <li>a user</li> <li>a school year</li> </ul> <p>So you would have the following routes:</p> <ul> <li><code>/users/{userId}/schoolyears</code> -> which corresponds to your first action</li> <li><code>/shoolyears/{id}</code> -> which corresponds to your second action</li> </ul> <p>So:</p> <pre><code>public class UsersController : ApiController { public SchoolyearBrowseResponse GetSchoolYears(int userId) { return _service.GetSchoolyears(userId); } } </code></pre> <p>and:</p> <pre><code>public class SchoolYearsController : ApiController { public SchoolyearOpenResponse Get(int id) { return _service.OpenSchoolyear(id); } } </code></pre> <p>and the final step is your routes:</p> <pre><code>public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "UserSchoolYears", routeTemplate: "api/{controller}/{userId}/schoolyears", defaults: new { controller = "Users", action = "GetSchoolYears" } ); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } </code></pre> <p>As a side note I would like to mention that a school year for a user (your <code>SchoolyearBrowseResponse</code>) class hardly makes sense. Normally for a given user you have a list of school years. And if you wanted to get a specific school year for a user you would use <code>/users/{userId}/schoolyears/{schoolyearid}</code>.</p>
 

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