Tuesday, October 1, 2013

Page Life Cycle of MVC Architecture.

We need an object on server side. In MVC world this is model. We need an instantiated model and pass it to the view so that views can extract the data from this object and create the final HTML markup.

Now the question would be, who would instantiate these Models. These models will be instantiated in the controllers. Controller will instantiate the models and then pass the model to the view so that the view can use them to generate the markup.

But now the bigger question, How would the controller be invoked  The controller will be invoked on the user request. All the user requests will be intercepted by some module and then the request URL will be parsed and based on the URL structure an appropriate controller will be invoked.


MVC Page Life cycle:

1.User sends the request in form of URL.
2.UrlRoutingModule intercepts the request and starts parsing it.
3.The appropriate Controller and handler will be identified from the URL by looking at the RouteTable collection. Also any data coming along with the request is kept in RouteData.
4.The appropriate action in the identified controller will be executed.
5.This action will create the Model class based on data.
6.The action will then pass on this created model class to some view and tell the view to prceed.
7.Now the view will then execute and create the markup based on the logic and Model's data and then push the HTML back to the browser.



A Look at Routing:

Now we have said that the controller and action will be decided by the URL. So it is perhaps a good idea to look at a very

simple example of this RouteTable entry so that we know what pattern of URL will invoked which controller and which action.

Here is the default entry in the RouteTable which is made in global.asax application_start event.
routes.MapRoute
(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Blog", action = "Index", id = UrlParameter.Optional
);


Here in this above route, the name for the route is specified as Default. The second argument specifies the pattern of the

URL that should lead to this route usage i.e. Any URL with pattern "SiteAddress/{controller}/{action}/{id}"  will use this

route. The final argument specifies that if this route will be used then a controller named Blog and an action Index will be invoked. id is optional meaning it could be present or not.

So the following URLs will invoke the respective actions and controllers.

Url: www.siteaddress.com 
Controller: Blog
Action: Index

Url: www.siteaddress.com/Blog 
Controller: Blog
Action: Index

Url: www.siteaddress.com/Blog/Create 
Controller: Blog
Action: Create

Url: www.siteaddress.com/Blog/Delete/1 
Controller: Blog
Action: Delete
Id: 1

Url: www.siteaddress.com/Category/Edit/1 
Controller: Category
Action: Edit
Id: 1

So the above examples would perhaps make it clear that what URL will invoke which controller and which action.

3 comments:

  1. Model: These are the classes that contain data. They can practically be any class that can be instantiated and can provide some data. These can be entity framework generated entities, collection, generics or even generic collections too.

    Controllers: These are the classes that will be invoked on user requests. The main tasks of these are to generate the model class object, pass them to some view and tell the view to generate markup and render it on user browser.

    Views: These are simple pages containing HTML and C# code that will use the server side object i.e. the model to extract the data, tailor the HTML markup and then render it to the client browser.

    ReplyDelete
  2. What U Need To Learn:

    * HTML
    * CSS
    * JQuery
    > JQuery UI
    > JQueryTemplates ( Transform JSON into HTML Markup )

    ReplyDelete
  3. Advantages of MVC

    1. Separation of Concerns
    2. Better Support For Test Driven Development
    3. Clean URL
    4. Better For Search Engine Optimization
    5. Full Control Over the User Interface or the HTML
    6. Loosely Coupled so Each Component can be tested independently.

    ReplyDelete