Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC simplify routing
    text
    copied!<p>How can I simplify these routes?</p> <pre><code>config.Routes.MapHttpRoute( name: "GetUsersRoute", routeTemplate: "api/User", defaults: new { controller = "User", action = "GetUsers" } ); config.Routes.MapHttpRoute( name: "GetUserRoute", routeTemplate: "api/User/{id}", defaults: new { controller = "User", action = "GetUser", id = RouteParameter.Optional } ); config.Routes.MapHttpRoute( name: "GetEmailAddressRoute", routeTemplate: "api/User/GetEmailAddress/{id}", defaults: new { controller = "User", action="GetEmailAddress", id = RouteParameter.Optional } ); /*** Default ***/ config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); </code></pre> <p>I needed to add the GetEmailAddressRoute because the controller didn't know which action to take for a path like <code>api/User/5</code>. However, since then former routes like <code>api/User</code> don't lead to the correct action.</p> <p>Some action signatures from the controller:</p> <pre><code>public Object GetUsers() { }; public Object GetUser(int id) { }; public HttpResponseMessage GetEmailAddress (int id) { }; </code></pre> <p>But now I'd need to add another route for the Post actions. Is there a simpler way?</p> <p><strong>Update</strong>: What helped me for posting new users was to remove the action from the first route. Now it is</p> <pre><code>config.Routes.MapHttpRoute( name: "GetUsersRoute", routeTemplate: "api/User", defaults: new { controller = "User" } ); </code></pre>
 

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