ASP.NET MVC: Traditional Routes


Before Attribute-based routing there is one method to write routes i.e. Traditional ways to write routes Routes. “Traditional Routes” means we can define routes only in RegisterRoute() method present in RouteConfig.cs. In this RouteConfig.cs file, by default one route mapping is specified when the application is created using the MVC template like below:

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

     routes.MapRoute(
           "Default", // Route name
           "{controller}/{action}/{id}", // URL with parameters
           new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
}

Basically, RegisterRoutes method is called from the Application_Start event of the application life cycle. means in the Global.asax. This event is called when the application starts running and our routes should be ready before any call comes to the application. So that we called RegisterRoutes from Application_Start event.

The IgnoreRoute method used for ignoring the route with whatever pattern passed to it. In the above example IgnoreRoute method used for ignoring axd file extensions.

In the above code, we can see there is one method MapRoutes(), in this method is used to register all routes to route table where our all route are present.
 -- The First parameter for this method is Route name which must be unique for each MapRoute() statement otherwise it gives exceptions like “A route named 'Default' is already in the route collection. Route names must be unique.”.
-- the Second parameter is used for URL means we can write URL pattern for all routes in your application.
-- And the third parameter is default values for URL parameters.

In the above code if we not specified any controller and action then it goes to default values means it execute the Index method from Home Controller.

If you want to add specific word before our real route you can add it directly like below:

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

            routes.MapRoute(
                "Signup", // Route name
                "Signup/{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }

In the above code, I have added another route, in that, I have simply added the signup keyword before our route then all URL work like:


If there is a case where you want to add only login keyword in URL like www.mvcstation.com/Login
For the login page, you can simply create another route for the login page and set default values for that
See below code for login page:

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

            routes.MapRoute(
                "Login", // Route name
                "Login", // URL with parameters
                new { controller = "Account", action = "Logon", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
}

If we add the route info like above in our RouteConfig.cs, and hit the login page using www.mvcstation.com/Login URL, it first finds the account controller then goes to the LogOn action method. 

There is another optional parameter for RegisterRoutes() method where we can add validation for the parameter in URL called Route constraint:

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

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new { id = @"\d+" }
            );
}

In the above example, I have added validation for the id parameter. We can add regex for any parameter defined in URL patterns like controller name, action name, and parameters.






Post a Comment

6 Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
    python training Course in chennai | python training in Bangalore | Python training institute in kalyan nagar

    ReplyDelete
  3. Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.
    java training in chennai | java training in bangalore

    java online training | java training in pune


    ReplyDelete
  4. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
    Best PHP Training Institute in Chennai|PHP Course in chennai

    Best .Net Training Institute in Chennai
    MCSE Training in Chennai
    AI Training in Chennai
    SEO Training in Chennai

    ReplyDelete