Sunday, 23 December 2018

How a requested URL processed in MVC

To understand, how a URL processed in the browser for an MVC application, we need to know a few things.

1. Controller
2. View
3. Model
4. Areas
5. Shared Folder
6. Global.asax
7. RouteConfig.cs

Let say we are requesting a URL http://localhost:2717/adminaccess/department/indexwe need to understand each part of the URL.






































Application execution starts from Global.asax file. When the URL is invoked it first checks for the route. Route or routes are defined in RouteConfig.cs file at App_Start folder.





















RouteConfig mapping is defined in Global.asax. Notice that in Global.asax we have RegisterRoutes() method. 

RouteConfig.RegisterRoutes(RouteTable.Routes);

As we are using Areas in our application, Areas are also defined in Global.asax.

AreaRegistration.RegisterAllAreas();


In RouteConfig.cs file we will find all available routes. In our case we have
only one route i.e. Default route. we can add as many as routes to RegisterRoutes
method. But make sure the Default route should always placed after all Routes.


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

In this example we are using Areas. Areas make your application more maintanable.
If you look into the Areas it contains its own Cotrollers, Views, Models,
Shared folder,WebConfig and AreaRegistration file.

 
In this URL

http://localhost:2717/adminaccess/department/index

adminaccess: Is the area name
department:  Is the Controller Name
index : Is the Controller Action Name

When we type this URL and hit enter it first check the Global.asax file for route and
if it found the route then check for the Area then checks for the Controller in that area
and then check for the controller action method defined in that Controller Class. When it 
founds all then it looks for the View that is associated with Controller Action Method.










No comments:

Post a Comment