What is Routing in ASP.NET Core and its types?
What is Routing in ASP.NET Core and its types?
Student
The Anubhav portal was launched in March 2015 at the behest of the Hon'ble Prime Minister for retiring government officials to leave a record of their experiences while in Govt service .
Simple Understanding
When a user hits:
Routing decides:
How Routing Works
Flow:
Example:
Mapped to:
Types of Routing in ASP.NET Core
There are mainly 2 types:
1. Conventional Routing
Defined centrally in
Program.csExample:
How it works:
Example URL:
Calls:
Pros:
Cons:
2. Attribute Routing
Defined directly on controllers/actions using attributes
Example:
URL:
Shortcut:
Pros:
Cons:
3. Endpoint Routing (Modern Approach)
Introduced in ASP.NET Core 3+
Configured in:
Works with:
4. Minimal API Routing (Advanced)
Used in lightweight APIs:
Route Parameters
Example:
controller→ requiredaction→ requiredid?→ optionalRoute Constraints
Restrict parameter types:
Default Values
Routing vs URL Rewriting
Best approach:
Common Mistakes
1. Conflicting routes
2. Missing
app.MapControllers()3. Overusing hardcoded routes
Real Example (Best Practice)
Final Definition