Route Attribute with default values
We can also provide a default value to Routing parameter as below
[Route("Sports/{category=12}")]
public ActionResult SomeAction(string category)
{
ViewBag.category = category;
return View();
}
Default Routes
We can create a default Route using Route Attribute using below syntax
[RoutePrefix("Sports")]
[Route("{action=SomeAction}")]
public class HomeController : Controller
{
//Sports/SomeAction/category
[Route("{category}")]
public ActionResult SomeAction(string category)
{
ViewBag.category = category;
return View();
}
//Games/1/edit
[Route("~/Games/{category}/edit")]
public ActionResult SomeMoreAction(string category)
{
ViewBag.category = category;
return View();
}
}
Naming and Accessing Route
Routes can be named as well. Here is how you can create a named Route
//Sports/SomeAction/category
[Route("{category}",Name = "Test")]
public ActionResult SomeAction(string category)
{
ViewBag.category = category;
return View();
}
A named route can be accessed from View as below
<a href="@Url.RouteUrl("Test")">Test</a>