Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I route a mix of optional and conditionally mandatory parameters in ASP.NET Web Api/MVC
    primarykey
    data
    text
    <p>I'm guessing that the answer is to make multiple routes, but our route list is beginning to get long, so I'm looking to see if this can be expressed more elegantly and simply. :)</p> <p>In my current example, I want to create a route that has multiple options, with some elements that are variable and some that are not.</p> <p>So an example URI would be:</p> <pre><code>http://localhost/api/schedules/10/instances/5 </code></pre> <p>And a matching route definition would be:</p> <pre><code>api/schedules/{id}/instances/{item} </code></pre> <p>The possible endpoints would be:</p> <pre><code>http://localhost/api/schedules/10 // returns schedule number 10 http://localhost/api/schedules/10/instances // returns all instances in schedule 10 http://localhost/api/schedules/10/instances/5 // returns 5th instance in schedule 10 </code></pre> <p>What seems to cause trouble is wanting to have a fixed keyword between the two optional parameters. I've tried the following definition (and several variants):</p> <pre><code>RouteTable.Routes.MapHttpRoute( "RouteName", "api/schedules/{id}/instances/{item}", new { id = RouteParameter.Optional, instances = RouteParameter.Optional, item = RouteParameter.Optional, controller = "SomeController" } ) </code></pre> <p>With that route, just hitting /schedules/10 is not recognized, and /schedules/10/instances and schedules/10/instances/5 both return the schedules/10/instances result.</p> <p>Is this possible in some way, or should I be making two routes, one for /schedules/{id} and one for /schedules/{id}/instances/{item}?</p> <p>As a side note, I'm working on a Web API project, but as far as I know, that's just a specific case of an MVC solution, so I tagged both as MVC and Web API.</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