Introduction

The rest of the article will describe different types of routing supported and the configurations needed in the levels of,

  1. Application execution pipeline (middleware)
  2. The controllers and action

Definitions

  1. Routing is responsible for matching incoming HTTP requests and dispatching those requests to the app's executable endpoints
  2. Endpoints are the app's units of executable request-handling code. Endpoints are defined in the app and configured when the app starts

Types of routing

Conventional Based Routing

Area Conventional Based Routing

Controller level

Routing In MVC Application

View Level

Routing In MVC Application

Application execution pipeline can be configured to enable area-based routing in either one of the following two ways.

Using a code similar to the previous one as follows,

endpoints.MapControllerRoute(name: "Area", pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}", defaults: new {
    area = "Home"
});

Using MapAreaControllerRoute method IEndpointRouteBuilderInterface as in the following example,

endpoints.MapAreaControllerRoute(pattern: "{area}/{controller}/{action}/{id?}", name: "areas", areaName: "Home", defaults: new {
    controller = "Home", action = "Index"
});

Mixed Conventional Based Routing

Attribute routing

Hopefully, we will discuss this in a later article.

Examples

Default routing type (conventional without area support).

configuration in the startup.cs

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

Result

Routing In MVC Application

Conventional with area support

Configuration in the startup.cs either,

endpoints.MapAreaControllerRoute(pattern: "{area}/{controller}/{action}/{id?}", name: "areas", areaName: "Home", defaults: new {
    controller = "Home", action = "Index"
});

or,

endpoints.MapControllerRoute(name: "Area", pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}", defaults: new {
    area = "Home"
});

Result

Routing In MVC Application

Mixed conventional

Example 1 - configuration

endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapAreaControllerRoute(pattern: "{area}/{controller}/{action}/{id?}", name: "areas", areaName: "Home", defaults: new {
    controller = "Home", action = "Index"
});

Result

Routing In MVC Application

Example 2 - Configuration

endpoints.MapAreaControllerRoute(pattern: "{area}/{controller}/{action}/{id?}", name: "areas", areaName: "Home", defaults: new {
    controller = "Home", action = "Index"
});
endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");

Result

Routing In MVC Application

Redirecting from an action in a controller not belonging to an area to an action belonging to an area.

From Controller this will work only if area conventional routing comes first in the startup.cs file as follows,

endpoints.MapAreaControllerRoute(pattern: "{area}/{controller}/{action}/{id?}", name: "areas", areaName: "Home", defaults: new {
    controller = "Home", action = "Index"
});
endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");

Otherwise, errors of too many directions will occur.

Routing In MVC Application

Through a hyperlink.

Example

Routing In MVC Application

Result

Routing In MVC Application

Conclusion

This article summarized different conventional routing implementation methods as well as ways to navigate between routes.

Resources