Understanding Routing in ASP.NET MVC

Introduction

When you open a webpage, you usually type a URL in the browser, such as: https://example.com/Home/Index

But have you ever wondered how ASP.NET MVC knows which controller and action method should run?

The answer is Routing.

Routing is one of the most important concepts in ASP.NET MVC because it connects a URL to a specific controller and action method.

In this beginner-friendly article, we will learn:

  • What Routing is

  • Why Routing is important

  • How Routing works in ASP.NET MVC

  • Default Routing structure

  • Simple examples to understand routing

Everything will be explained in easy words for beginners.

What is Routing?

Routing is a mechanism that maps incoming URLs to controller actions in an ASP.NET MVC application.

In simple words:

Routing tells the application which controller and method should handle a specific URL request.

Example

URL:

/Home/Index

Routing understands this as:

  • Controller → HomeController

  • Action → Index()

Why Routing is Important

Routing provides many benefits in web development.

1 Clean URLs

Instead of complicated URLs like:

example.com/page?id=10

MVC routing allows clean URLs like:

example.com/Product/Details/10

2 Better SEO

Search engines prefer clean and readable URLs.

3 Easy Navigation

Routing makes URLs easier for users to understand.

Default Routing in ASP.NET MVC

In ASP.NET MVC, routing is configured in the file:

RouteConfig.cs

Location:

App_Start → RouteConfig.cs

Default routing code looks like this:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Understanding the Default Route

Let's break this route into parts.

{controller}/{action}/{id}

Controller

This represents the controller name.

Example:

ProductController

Action

This represents the method inside the controller.

Example:

Details()

Id

This represents an optional parameter.

Example:

/Product/Details/10

Here:

  • Controller = Product

  • Action = Details

  • Id = 10

Example of Routing in MVC

Create a controller.

public class ProductController : Controller
{
    public ActionResult Details(int id)
    {
        ViewBag.ProductId = id;
        return View();
    }
}

Now open this URL in the browser:

/Product/Details/5

The routing system will call:

  • ProductController → Details(5)

View Example

Inside the view file:

<h2>Product ID: @ViewBag.ProductId</h2>

Output:

Product ID: 5

Custom Routing Example

Sometimes developers want custom URLs.

Example:

/products/all

Instead of:

/Product/List

We can create custom routing.

routes.MapRoute(
    name: "ProductList",
    url: "products/all",
    defaults: new { controller = "Product", action = "List" }
);

Now this URL works:

/products/all

Attribute Routing

Modern ASP.NET MVC also supports Attribute Routing.

Instead of defining routes in RouteConfig, we can define them directly in the controller.

Example:

[Route("products/details/{id}")]
public ActionResult Details(int id)
{
    return View();
}

Now the URL becomes:

/products/details/10

Advantages of Routing

Routing provides several advantages.

  • Clean URL structure

  • URLs become easier to read.

  • Flexible navigation

  • Developers can design URLs according to application needs.

  • Better SEO performance

  • Search engines prefer structured URLs.

Common Mistakes Beginners Make

Many beginners face routing issues because of these mistakes:

  • ❌ Controller name mismatch

  • ❌ Incorrect action method name

  • ❌ Wrong parameter types

  • ❌ Route order problems

Always check controller, action, and parameter names carefully.

Real-World Example

Consider an e-commerce website.

Different URLs might look like this:

/products
/products/details/5
/cart
/orders/history

Routing ensures each URL opens the correct controller and action method.

Conclusion

Routing is a core feature of ASP.NET MVC that connects URLs with controller actions.

Without routing, the application would not know which code should handle a user request.

Understanding routing helps developers create:

  • Clean URLs

  • Better user navigation

  • SEO-friendly web applications