ASP NetMVC Core MVC Customizing Routes - SkillBakery Studios

Breaking

Post Top Ad

Post Top Ad

Friday, July 10, 2026

ASP NetMVC Core MVC Customizing Routes

ASP NetMVC Core MVC Customizing Routes

Screenshot from the tutorial
Screenshot from the tutorial

Customizing Routes in ASP.NET Core MVC: A Quick Guide

ASP.NET Core MVC is a powerful framework for building web applications. One of its key features is routing, which allows you to define how URLs map to your application's actions. In this post, we will explore how to customize routes in ASP.NET Core MVC, enhancing your application's URL structure and improving user experience.

Understanding Routing in ASP.NET Core MVC

Routing in ASP.NET Core MVC is the mechanism that matches incoming requests to the corresponding controller actions. By default, ASP.NET Core uses a convention-based routing system, which means that it follows a set of rules to determine the routes based on the URL structure and controller names. However, you can customize this behavior to meet your specific requirements.

Default Routing Convention

When you create a new ASP.NET Core MVC application, the default routing convention is usually defined in the Startup.cs file within the Configure method:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseRouting();

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

In this example:

  • The default route pattern is defined as {controller=Home}/{action=Index}/{id?}.
  • This means that if no specific controller or action is provided in the URL, the application will default to the HomeController and the Index action.

Customizing Routes

Now, let’s dive into how to customize these routes to make your application more intuitive and user-friendly.

Adding Custom Routes

You can add your custom routes by defining new patterns in the UseEndpoints method. Here’s an example of how to do that:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "products",
            pattern: "products/{id}",
            defaults: new { controller = "Products", action = "Details" });

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

In this example:

  • We added a custom route named products, which maps URLs like /products/1 to the Details action of the ProductsController.
  • The id parameter in the URL is passed to the action method, allowing you to dynamically retrieve product details based on the ID.

Using Route Attributes

In addition to convention-based routing, ASP.NET Core MVC supports attribute routing, which allows you to define routes directly on your controller actions. This approach can make your routing more readable and maintainable.

Here’s how you can implement attribute routing:

[Route("products")]
public class ProductsController : Controller
{
    [HttpGet("{id}")]
    public IActionResult Details(int id)
    {
        // Retrieve product details by id
        return View();
    }

    [HttpGet]
    public IActionResult Index()
    {
        // Retrieve all products
        return View();
    }
}

In this example:

  • The ProductsController is decorated with the [Route("products")] attribute, which sets a base route.
  • The Details action handles GET requests to /products/{id}, while the Index action handles GET requests to /products.

Route Constraints

You can also use route constraints to enforce specific rules on your routes. For instance, you might want to ensure that the id parameter is always an integer. You can achieve this by specifying a route constraint like so:

endpoints.MapControllerRoute(
    name: "products",
    pattern: "products/{id:int}",
    defaults: new { controller = "Products", action = "Details" });

In this case, the route will only match if the id parameter is an integer.

Conclusion

Customizing routes in ASP.NET Core MVC allows you to design user-friendly and SEO-optimized URLs for your web application. By leveraging both convention-based and attribute routing, along with route constraints, you can create a flexible and maintainable routing structure.

Feel free to explore these options in your next ASP.NET Core MVC project, and don't hesitate to experiment with different routing patterns to see what works best for your application's needs. Happy coding!

Another screenshot from the tutorial
Another view from the tutorial

Connect with SkillBakery Studios

Explore more tutorials, tools, and resources:

Posted by SkillBakery Studios

No comments:

Post a Comment

Post Top Ad