You can make a URI parameter optional by adding a question mark to the route parameter. You can also specify a default value by using the form parameter=value.
public class BooksController : Controller
{
// eg: /books
// eg: /books/1430210079
[Route(“books/{isbn?}”)]
public ActionResult View(string isbn)
{
if (!String.IsNullOrEmpty(isbn))
{
return View(“OneBook”, GetBook(isbn));
}
return View(“AllBooks”, GetBooks());
}
// eg: /books/lang
// eg: /books/lang/en
// eg: /books/lang/he
[Route(“books/lang/{lang=en}”)]
public ActionResult ViewByLanguage(string lang)
{
return View(“OneBook”, GetBooksByLanguage(lang));
}
In this example, both /books and /books/1430210079 will route to the “View” action, the former will result with listing all books, and the latter will list the specific book. Both /books/lang and /books/lang/en will be treated the same.