Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to tell routing that my default action name is equal to the HTTP Action Verb?
    primarykey
    data
    text
    <p>I've looked through <a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection" rel="nofollow">this document</a> on MSDN and can't come up with the answer.</p> <p>Considering that I have a route defined like this:</p> <pre><code> config.Routes.MapHttpRoute( name: "DefaultWithActionAndID", routeTemplate: "v{version}/{controller}/{action}/{id}", defaults: null, constraints: new { action = @"[a-zA-Z]+", id = @"\d+" } ); config.Routes.MapHttpRoute( name: "DefaultWithID", routeTemplate: "v{version}/{controller}/{id}", defaults: null, constraints: new { id = @"\d+" } ); config.Routes.MapHttpRoute( name: "DefaultWithoutActionOrId", routeTemplate: "v{version}/{controller}", defaults: null, ); </code></pre> <p>Now I have two controllers that looks like this:</p> <pre><code>public class ItemController:ApiController{ [HttpGet] public Item Get(int id){} [HttpGet] public Item GetSomething(int id){} [HttpPut] public Item Put(Item newItem){} } public class AnotherController:ApiController{ [HttpPut] public HttpResponseMessage Put(Something item){} } </code></pre> <p>I'd like to be able to call all of these endpoints like this:</p> <pre><code>GET /api/Item/344 GET /api/Item?id=344 GET /api/Item/Something/2334 GET /api/Item/Something?id=2334 PUT /api/Item body={newItem} PUT /api/Another body={newSomething} </code></pre> <p>This will work, but only if I add "Get" as the default action name. If I do not specify a default action name in my route, then it complains about multiple matching action names. If I do add the default action name, then I cannot call PUT to the Put() method without an error because the action name doesn't match the default and isn't found.</p> <pre><code> // Will work in some cases, but not all config.Routes.MapHttpRoute( name: "DefaultWithID", routeTemplate: "v{version}/{controller}/{id}", defaults: new { action="Get", id=RouteParameters.Optional }, constraints: new { id = @"\d+" } ); // Works GET /api/Item/344 GET /api/Item?id=344 GET /api/Item/Something/2334 GET /api/Item/Something?id=2334 // Doesn't work PUT /api/Item body={newItem} PUT /api/Another body={newSomething} </code></pre> <p>How can I tell Routing to use the Action with the name that matches my HTTP Verb, if one exists before trying to use </p>
    singulars
    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.
    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