Sobes.tech
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:

  1. 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) { ... }
    
  2. Convention-based Routing

    • Defined in a configuration file or in the Startup.Configure method.
    • For example, in ASP.NET Core:
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
    
  3. Custom Route Handlers

    • You can create custom route handlers for more flexible logic.
  4. 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.