- ASP.NET Core - Log In and Log Out
- ASP.NET Core - Create a User
- ASP.NET Core - User Registration
- ASP.NET Core - Identity Migrations
- Identity Configuration
- ASP.NET Core - Authorize Attribute
- ASP.NET Core - Identity Overview
- ASP.NET Core - Razor Edit Form
- ASP.NET Core - Razor Tag Helpers
- ASP.NET Core - Razor View Import
- ASP.NET Core - Razor View Start
- ASP.NET Core - Razor Layout Views
- ASP.NET Core - DBContext
- Setup Entity Framework
- ASP.NET Core - Views
- ASP.NET Core - Action Results
- ASP.NET Core - Attribute Routes
- ASP.NET Core - Routing
- ASP.NET Core - MVC Design Pattern
- ASP.NET Core - Setup MVC
- ASP.NET Core - Static Files
- ASP.NET Core - Exceptions
- ASP.NET Core - Middleware
- ASP.NET Core - Configuration
- ASP.NET Core - Project.Json
- ASP.NET Core - Project Layout
- ASP.NET Core - New Project
- ASP.NET Core - Environment Setup
- ASP.NET Core - Overview
- ASP.NET Core - Home
ASP.NET Core Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
ASP.NET Core - Attribute Routes
In this chapter, we will learn another approach to routing and that is attribute-based routing. With attribute-based routing, we can use C# attributes on our controller classes and on the methods internally in these classes. These attributes have metadata that tell ASP.NET Core when to call a specific controller.
It is an alternative to convention-based routing.
Routes are evaluated in the order that they appear, the order that you register them in, but it s quite common to map multiple routes particularly if you want to have different parameters in the URL or if you want to have different pterals in the URL.
Example
Let us take a simple example. Open the FirstAppDemo project and run the apppcation in the browser. When you specify /about, it will produce the following output −
What we want here is that when we specify /about, the apppcation should invoke the Phone action of the AboutController. Here, we can enforce some exppcit routes for this controller using a Route attribute. This attribute is in the namespace Microsoft.AspNet.Mvc.
The following is the implementation of AboutController in which the attribute routes are added.
using Microsoft.AspNet.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace FirstAppDemo.Controllers { [Route("about")] pubpc class AboutController { [Route ("")] pubpc string Phone() { return "+49-333-3333333"; } [Route("country")] pubpc string Country() { return "Germany"; } } }
Here we want this route to look pke about and for the Phone action we have specified an empty string, which means that we don t need the action to be specified to get this method. The user just needs to come to /about. For the Country action we have specified the “country” in the route attribute. Let us save the AboutController, refresh your browser and go to the /about and should give you the Phone action.
Let us specify the /about/country. This will allow you to get to that Country action.
If you want a segment of the URL to contain the name of your controller, what you can do is instead of using the controller name exppcitly, you can use a token controller inside the square brackets. This tells ASP.NET MVC to use the name of this controller in this position as shown in the following program.
using Microsoft.AspNet.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace FirstAppDemo.Controllers { [Route("[controller]")] pubpc class AboutController { [Route ("")] pubpc string Phone() { return "+49-333-3333333"; } [Route("[action]")] pubpc string Country() { return "Germany"; } } }
This way, if you ever rename the controller, you don t have to remember to change the route. The same goes for an action and imppcitly there is a slash (/) between the controller and the action. It is a hierarchical relationship between the controller and the action just pke it is inside the URL. Let us save this controller again. Most probably, you will see the same results.
Let us specify the /about/country. This will allow you to get to that Country action.
Advertisements