.NET Core | Routing

Karim Samir
simplifycoding
Published in
Mar 27, 2024

The ASP.NET Core Apps uses the Route to go to the controller action.

Each Route contains a Name, URL Pattern (Template), Defaults and Constraints. The URL Pattern is compared to the incoming URLs for a match.

An example of URL Pattern is:

{controller=Home}/{action=Index}/{id?}

Based Routing

Attribute Routing

Uses the attributes defined directly on the controller action to define the routes.

Action Verbs

Attribute routes can also be used with HTTP verb attributes like HttpGet, HttpPost etc.

Route Constraints in ASP.NET Core

Ex1: Regular Express Route Constraint: In the following example, we are using regular expression to restrict the Year value to 4 digits.

A request for /Home/Index/2017 will match the above route, but request for /Home/Index/20 will fail.

Ex 2: alpha (Accepts only alphabets)

--

--