</>
Skip to content
ASP lessons (34/40)

ASP — ASP.NET Routing

Attribute Routing

[Route("api/users")]
[ApiController]
public class UsersController : ControllerBase
{
    [HttpGet]
    public IActionResult GetAll() { }
    
    [HttpGet("{id}")]
    public IActionResult GetById(int id) { }
    
    [HttpPost]
    public IActionResult Create(User user) { }
}

Convention Routing

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

Route Parameters

[HttpGet("users/{id:int}")]
public IActionResult GetUser(int id) { }

[HttpGet("users/{name:alpha}")]
public IActionResult GetUserByName(string name) { }

Route Constraints

ConstraintDescription
intInteger
stringString
boolBoolean
datetimeDateTime
decimalDecimal

Mini Practice

  1. Use attribute routing
  2. Add route constraints
  3. Create RESTful routes
  4. Handle optional parameters

Up Next

Continue with ASP.NET Middleware — middleware.

Related Topics

Frequently Asked Questions about ASP.NET Routing

What is ASP.NET Routing in ASP?

ASP.NET Routing is a fundamental concept in ASP. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn ASP.NET Routing?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn ASP.NET Routing.

Why is ASP.NET Routing important in ASP?

ASP.NET Routing is essential for ASP development. Understanding this concept will help you write better code and solve real-world problems more effectively.