In ASP.NET Core MVC, routing is the process that determines which controller and action should handle an incoming HTTP request. When a user sends a request (such as navigating to a URL), the MVC routing system maps the URL to a corresponding controller and action method. The routing system defines how the request should be processed and where the response should come from.
Routing works by matching a URL pattern to a route template that corresponds to a controller and an action. The default routing convention in ASP.NET Core MVC is defined as follows:
/Controller/Action/ParameterFor example:
https://example.com/Books/Details/1In this example:
In an ASP.NET Core MVC application, routes are typically defined in the Startup.cs file in the Configure method, using the UseEndpoints method.
By default, ASP.NET Core uses the following pattern for routing:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
controller=Home: Specifies that if no controller is provided in the URL, it will default to the HomeController.action=Index: If no action is provided, it will default to the Index action.{id?}: This represents an optional parameter (often an ID) that might be passed to the action. The ? indicates that the id parameter is optional.Given the default routing configuration:
https://localhost:5000/Books/Details/5
BooksControllerDetailsid = 5The BooksController will handle the request, and the Details action will receive the id parameter, which is 5.
public class BooksController : Controller
{
public IActionResult Details(int id)
{
// Fetch and return the book with the specified id
var book = _bookService.GetBookById(id);
return View(book);
}
}
You can create routes with optional parameters.
URL: https://localhost:5000/Books/Details/5
id is provided, it will call the Details action with the id.URL: https://localhost:5000/Books/Details
id is not provided, it will call the Details action without the id.The route template in the Configure method:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
{id?} makes the id parameter optional.While the default route convention works for most cases, you may want to define custom routes to achieve cleaner, more descriptive URLs.
Let's say you want to create a custom route for searching books by title:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "search",
pattern: "Books/Search/{title}",
defaults: new { controller = "Books", action = "Search" });
});
Now, the URL pattern Books/Search/{title} will map to the Search action in BooksController, where title is a parameter.
https://localhost:5000/Books/Search/CSharp
BooksControllerSearchtitle = CSharppublic class BooksController : Controller
{
public IActionResult Search(string title)
{
var books = _bookService.SearchBooksByTitle(title);
return View(books);
}
}
You can also add constraints to route parameters. For instance, let's define a route that only matches if the id is a number:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "idConstraint",
pattern: "Books/Details/{id:int}",
defaults: new { controller = "Books", action = "Details" });
});
In this case, the route will only match if the id is an integer. If a non-integer is provided (e.g., Books/Details/abc), the route will not match, and an error will be returned.
When defining actions in your controllers, the parameters in the URL (such as id) are automatically mapped to the method parameters in the action method.
For example, if you define the following route in Configure:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
And in your controller, you define the following action:
public class BooksController : Controller
{
public IActionResult Details(int id)
{
var book = _bookService.GetBookById(id);
return View(book);
}
}
When the URL Books/Details/5 is requested, the id parameter in the URL (5) will automatically be passed to the Details action method.
Attribute routing provides a way to define routes directly on controller actions using attributes. This method is more flexible and allows for greater control over route definitions.
public class BooksController : Controller
{
[Route("Books/Details/{id}")]
public IActionResult Details(int id)
{
var book = _bookService.GetBookById(id);
return View(book);
}
[Route("Books/Search/{title}")]
public IActionResult Search(string title)
{
var books = _bookService.SearchBooksByTitle(title);
return View(books);
}
}
In this case:
Books/Details/{id} will map to the Details action.Books/Search/{title} will map to the Search action.Routing in ASP.NET Core MVC is the mechanism that maps URLs to specific controllers and actions. By default, routing follows a pattern of {controller}/{action}/{id}, but you can customize routes to match your application's needs using attribute routing or custom route templates. MVC routing enables flexible and clean URL designs that make it easier for users and developers to interact with your application.
Open this section to load past papers