Default Route in ASP.NET MVC


What is Default Route?

The Default Route is the standard routing pattern that ASP.NET MVC uses to map URLs to Controllers and Action methods.

It is automatically created when you create a new MVC project.


Default Route Pattern

{controller}/{action}/{id}

Default Route Code (RouteConfig.cs)

RouteConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Your_Project_Name
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
               routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

Understanding the Default Route

  • controller → Name of the Controller
  • action → Action Method inside Controller
  • id → Optional Parameter

Example 1

URL:

https://localhost:1234/Student/Details/5

Explanation:

  • Student → StudentController
  • Details → Details() method
  • 5 → id parameter

Example 2 (When URL is Empty)

URL:

https://localhost:1234/

Since no controller and action are given, MVC uses default values:

  • Controller → Home
  • Action → Index

So it automatically calls:

HomeController  Index()

Simple Real-Life Example

Imagine a school:

  • School Gate → URL
  • Department → Controller
  • Teacher → Action Method
  • Student Roll Number → ID

If you don’t mention department, the school sends you to the Main Office (Home/Index).


Important Points

  • Default route follows {controller}/{action}/{id}
  • ID is optional
  • Home/Index is default page
  • It works automatically in every MVC project

Summary

✔ Default route connects URL to Controller & Action
✔ Pattern: {controller}/{action}/{id}
✔ id is optional
✔ Home/Index is default fallback