Interviews3 answers
What is the difference between convention-based routing and attribute routing? When would you use each?
3 Answers
in simple word
convention routing: global route setup which is in rout cofig
attribute route: custome route setup on controller ot action method
Dashrath HapaniPosted Jan 27, 2025, 10:20 PM EST
Convention-Based Routing
Definition: Defines routing patterns in a central RouteConfig file (e.g., routes.MapRoute in RouteConfig.cs).
Configuration: Routes are applied globally, based on predefined patterns.
Flexibility: Less flexible for complex or specific scenarios.
Readability: Routes are separated from controllers/actions, making them harder to trace.
When to Use:
Simple, consistent routing patterns across the application.
Legacy MVC applications.
Attribute Routing
Definition: Defines routes directly on controller actions using attributes (e.g., [Route("api/products/{id}")]).
Configuration: Routes are defined locally on controllers and actions.
Flexibility: Highly flexible, allowing specific, fine-grained route definitions.
Readability: Easier to trace routes as they are defined alongside the actions.
When to Use:
APIs and complex routing scenarios.
Applications requiring custom routes for specific actions.
Projects with microservices or RESTful services.
Shubham SidnalePosted Jan 5, 2025, 5:20 AM EST
- Convention-Based Routing is Centralized in configuration files while Attribute Routing is Defined directly on controllers and actions.
- Convention-Based routes are managed in one place while Attribute Routes are spread across controllers.
- Convention-Based Routs are Simple, consistent URL structures while Attribute Routes are User-friendly URLs, RESTful APIs.
Use Convention-Based Routing When:
Your application follows a straightforward and consistent structure.
You prefer managing all routes centrally for simplicity.
The URLs naturally align with the controller/action pattern.
Use Attribute Routing When:
You require greater control over the URL design.
Your application demands custom, user-friendly, or SEO-friendly URLs.
You’re developing RESTful APIs or need routes that don’t conform to standard conventions.
Jignesh KumarPosted Dec 28, 2024, 1:32 PM EST



