Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Route Config in Asp.Net MVC

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 456
    Comment on it

    "Basics of Route Config in Asp.Net MVC"

        Routing is one of the primary concern of MVC. In MVC we have to map URL to a particular Action method on a particular Controller, the Action Method is then executed and returns an instance of ActionResult.

    Let us see how it all works:

    In MVC, routing is done by registering route templates which decide how the URLs will be mapped to a specific Action in a Controller.
    This registering of route templates is done in Global.asax file, which contains a single method Application_Start. To make the application working we call various configuration methods inside this method. Among all these calls one call is made to the RegisterRoutes method of the RouteConfig class (you can find this class under the App_Start folder in the Solution Explorer).

    Example: Global.asax

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Optimization;
    using System.Web.Routing;
    
    namespace RoutePOC
    {
        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
        }
    }
    

    In the above code you can see that a call is made to the RegisterRoutes method, which will populate the routes collection with pre-defined route templates for the application.

    The RouteConfig.cs class contains the default configured route of a MVC application:

    Example: RouteConfig.cs

    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 }
              );
        }
    }
    

    Note:-> Inorder to make a MVC website function there should be atleast one route map configured inside the
                      RouteConfig.cs class.

    In the code above we can see the route map with the name Default, it signifies that incase if no Controller and Action is provided or no matching Controller and Action in the application is provided, the control will be redirected to the "Index" Action method inside the "Home" Controller (Hence this will be called the Default URL for the Application).
    The parameter in the above code "id = UrlParameter.Optional" means that providing id is not mandatory in the URL. So if we wan't to access any Action Method inside the Controller we have to write the URL "ControllerName/ActionName" and if that Controller or Action method is not found the Default will be called i.e ("Index" Action method inside the "Home" Controller).

    Incase the Action method contains the parameters then we can write "ControllerName/ActionName/id" where id the value of the supplied parameter.

    Happy Coding...!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: