In this post we are going to discuss about the new feature of routing in ASP.NET MVC5 known as Attribute Routing. Attributes Routing are used to define the routes in controller/action directly. The main purpose of attribute routing is to control the URLs by define directly on action and the controller in the application.
When to use?
In some scenario, convention-based routing is complex to support certain URL patterns. This can achieve easily by using Attribute Routing.
For example, if user have ordering books based on authors, items based on color and so on. Then URL will look like /user/1/orders, this type of URL is difficult to match using the convention-based routing. But it can be achieve by convention-based routing but it will take our extra effort for this. By using attribute routing, it can be done much easier, by defining on the controller/action.
Defining Attribute Routing in ASP.NET MVC
Attribute routing can be define in two ways
- Controller level Attribute Routing
- Action level Attribute Routing
Register Attribute Routing
Attribute Routing use RoutePrefix and Route attributes to achieve the routing. The RoutePrefix attribute is used on the controller to specify a route prefix for all actions inside the specific controller. The Route attribute used on action method to allows us to specify the route on the action method. Before that we need to register attribute routing in the RouteConfig.cs using routes.MapMvcAttributeRoutes().
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
//Register Attribute Based Routing
routes.MapMvcAttributeRoutes();
//convention-based routing
routes.MapRoute(
name: “Default”,
url: “{controller}/{action}/{id}”,
defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }
);
}
Controller level Attribute Routing : [RoutePrefix()]
Controller level attribute will apply to all actions which is present within the controller unless a specific attribute route is added to an action.
[RoutePrefix(“dotnet-helpers”)]
[Route(“{action=index}”)] //default action
public class HomeController : Controller
{
//route: /dotnet-helpers/Index
public ActionResult Index()
{
return View();
}
}
OUTPUT :
Action level Attribute Routing : [Route()]
Action level Attribute routing will be for specific action with in the controller.
[RoutePrefix(“dotnet-helpers”)]
[Route(“{action=index}”)]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[Route(“About”)]
public ActionResult Contact()
{
ViewBag.Message = “Your contact page.”;
return View();
}
}
OUTPUT :
How to use Attribute Route in Area : [RouteArea()]
RouteArea attribute is used to define attribute routing for a controller which is present within Area.
[RouteArea(“SuperAdmin”)]
[RoutePrefix(“dotnet-helpers”)]
[Route(“{action=index}”)]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[Route(“About”)] //default action
public ActionResult Contact()
{
ViewBag.Message = “Your contact page.”;
return View();
}
}
Note :
- routes.MapMvcAttributeRoutes() : It will map all the routes that is defined as attributes.
- The main advantage of using Attribute routing is the flexibility that give us the excellent control over URL.
Thanks very nice blog!
Peculiar article, exactly what I needed.