Junior — Middle
What methods can be used to configure routing in a system?
sobes.tech AI
Answer from AI
In C#, routing can be configured in several ways depending on the framework and architecture used:
-
Attribute Routing
- Used in ASP.NET MVC and Web API.
- Routes are defined directly above controller methods using attributes like
[Route],[HttpGet],[HttpPost], etc.
[Route("api/products/{id}")] public IActionResult GetProduct(int id) { ... } -
Convention-based Routing
- Defined in a configuration file or in the
Startup.Configuremethod. - For example, in ASP.NET Core:
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); - Defined in a configuration file or in the
-
Custom Route Handlers
- You can create custom route handlers for more flexible logic.
-
Using Middleware
- In ASP.NET Core, routing can be implemented through middleware, intercepting requests and redirecting them.
The choice of method depends on the project requirements and the stack used.