This tutorial aims to help you understand the fundamentals of routing and controllers in ASP.NET. By the end of this tutorial, you should be able to define routes and create controllers that handle user requests and return responses.
Routing: Routing is a mechanism in ASP.NET that decides which action method of a controller class to execute. It maps URL routes to specific controllers and action methods.
Controllers: Controllers are classes in ASP.NET MVC that respond to HTTP requests. They determine what action to take, handle user input, work with models, and select a view to render that displays UI.
Startup.cs
file, the Configure()
method sets up the application's routes. Here's a basic example:app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
In this example, the pattern {controller=Home}/{action=Index}/{id?}
defines a default route where Home
is the default controller, Index
is the default action, and id
is an optional parameter.
HomeController
:public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
This HomeController
has an Index
action method that returns a view.
[Route]
attribute for more control over your routes.Here's how to define a custom route:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "blog",
pattern: "blog/{year}/{month}/{day}",
defaults: new { controller = "Blog", action = "Post" });
});
This route will match URLs like /blog/2022/10/01
and map them to the Post
action method of the Blog
controller.
Here's an example of attribute routing in a controller:
[Route("api/[controller]")]
public class OrdersController : Controller
{
[HttpGet("{id}")]
public IActionResult GetOrder(int id)
{
// Fetch and return the order with the specified id
}
}
In this example, the [Route]
attribute specifies the controller's route, and the [HttpGet]
attribute specifies the route of the action method.
These code snippets don't produce a visual output, but they set up the routing and control flow of an ASP.NET application.
Products
controller that maps the URL /products/{category}
to an action method Category
.UsersController
with an action method Profile
that responds to the URL /users/profile/{username}
.PostsController
that includes action methods for creating, reading, updating, and deleting blog posts.app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "productCategory",
pattern: "products/{category}",
defaults: new { controller = "Products", action = "Category" });
});
UsersController
can be created like this:public class UsersController : Controller
{
[Route("users/profile/{username}")]
public IActionResult Profile(string username)
{
// Fetch and return the profile of the specified user
}
}
PostsController
can be defined using attribute routing as follows:[Route("api/[controller]")]
public class PostsController : Controller
{
[HttpPost]
public IActionResult Create(Post post)
{
// Create a new post
}
[HttpGet("{id}")]
public IActionResult Read(int id)
{
// Fetch and return the post with the specified id
}
[HttpPut("{id}")]
public IActionResult Update(int id, Post post)
{
// Update the post with the specified id
}
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
// Delete the post with the specified id
}
}