Route Constraints
We can restrict the parameters used in routes via Route constraints. There are many route constraints provided by Asp.net mvc and you can create your custom constraints as well:
Examples:
//Sports/SomeAction/category
[Route("{category:int}",Name = "Test")]
public ActionResult SomeAction(int category)
{
ViewBag.category = category;
return View();
}
Also multiple constraints can be applied as below
[Route("~/Games/{category:int:min(2)}/edit")]
public ActionResult SomeMoreAction(int category)
{
ViewBag.category = category;
return View();
}
We can also make it as optional argument but that should come in last
//Sports/SomeThirdAction/category
[Route("{category:maxlength(5)?}", Name = "Test")]
public ActionResult SomeThirdAction(string category)
{
ViewBag.category = category;
return View();
}