asp.net core 2.0 attribute routing
In asp.net core routing is a way to define custom routes to call various controllers and their action. In asp.net , there are two ways to define routes.
- Conventional based routing. (If you are not aware of conventional routing then read my previous article.
- Attribute-Based Routing
Attribute-based routing is capable to handle complex routes. Routes definitions are provided on controller and action level , we will not define routes in startup.cs just like conventional based routing.
[Route("Home/Index")]
public IActionResult Index() { }
Here you can see that Index is decorated with Route attribute, It means that Index action may be called using http://localhost:3333/Home/Index
Note : Allways add app.UseMvcWithDefaultRoute(); into configure method of StartUp.cs
Example of Attribute based routing in asp.net core 2.2
// Route("MyHome") implies that this action may be called using MyHome
//[Route("MyHome")]
// [Route("")] imlies that no need to define controller name in URL
// [Route("")] For Empty Controller Name
[Route("[controller]")] // controller is just placeholder to get current place holder
public class HomeController : Controller
{
[Route("Index")]
public IActionResult Index()
{
return Content("Index Action of Home Controller");
}
[Route("Contact")]
public IActionResult ContactUs()
{
return Content("ContactUs Action of Home Controller");
}
}
}
Multiple Routes for an action:
As single action may have multiple routes, so these attribute based routing help us to write multiple routes URL to access the same action.
Example:
// Add Multiple Routes
[Route("feedback1")] // Access Using https://localhost:44347/Home/feedback1
[Route("")] //https://localhost:44347/Home
[Route("myfeedback")] // https://localhost:44347/Home/myfeedback
[Route("my/myfeedback")] // https://localhost:44347/Home/my/myfeedback
[Route("[action]")] // // https://localhost:44347/Home/feedback
public IActionResult Feedback()
{
return Content("FeedBack Action of Home Controller");
}
Attribute routing route constraints
Just like conventional based routing, we are also capable to write route constraint, route constraints are a kind of URL pattern, for example if we want that ID must be numeric, then It must be number.
// Route Constraints
[Route("calculate/{x:int}/{y:int}")] // https://localhost:44347/Home/Calculate/85/30
public IActionResult Add(int x, int y)
{
return Content("Addition is = " + (x + y));
}
Here you can see that x and y two route parameter only accept integer values otherwise it will throw 404 error.
Attribute routing in areas:
Attribute routing can be applied on areas of asp.net core . To understand this concept, first of all, we have to create an area in the working project.
namespace Core_Basic.Areas.Admin.Controllers
{
[Area("Admin")]
[Route("")]
public class DashBoardController : Controller
{
[Route("[area]/Login")] // https://localhost:44347/Admin/Login
public IActionResult Index()
{
return Content("Index Action of Admin Area DashBoard Controller");
}
}
}
In areas attribute routing we must decorate controller of the area using Area attribute.After decorating controller decoration we also decorate actions with area token([Route(“[area]/Login”)]) .