You can make a URI parameter optional by adding a question mark to the route parameter. If a route parameter is optional, you must define a default value for the method parameter.api
public class BooksController : ApiController { [Route("api/books/locale/{lcid:int?}")] public IEnumerable<Book> GetBooksByLocale(int lcid = 1033) { ... } }
In this example, /api/books/locale/1033
and /api/books/locale
return the same resource.app
Alternatively, you can specify a default value inside the route template, as follows:less
public class BooksController : ApiController { [Route("api/books/locale/{lcid:int=1033}")] public IEnumerable<Book> GetBooksByLocale(int lcid) { ... } }
This is almost the same as the previous example, but there is a slight difference of behavior when the default value is applied.ide
(In most cases, unless you have custom model binders in your pipeline, the two forms will be equivalent.)ui