Overriding a Route Prefix
We can override a Route prefix applied at controller level by using “~” character with Route attribute
[RoutePrefix("Sports")]
public class HomeController : Controller
{
[Route("{category}")]
public ActionResult SomeAction(string category)
{
ViewBag.category = category;
return View();
}
[Route("~/Games/{category}/edit")]
public ActionResult SomeMoreAction(string category)
{
ViewBag.category = category;
return View();
}
}
Route Attribute with Optional parameters
In conventional routing we make parameters optional by specifying UrlParameter.Optional in Route.Config file
In Attribute based routing we can achieve same effect by using “?” operator after parameter as below
[Route("{category?}")]
public ActionResult SomeAction(string category)
{
ViewBag.category = category;
return View();
}