Understanding Controllers Basics by Examples and Tutorial

In this chapter, you will learn:
1. Understanding basics of ASP.NET MVC Controllers.
2. Real World Controllers Example
3. Some Important Fact while using Controllers
4. How to Add Controllers
5. Understanding action results

Understanding Basics of MVC Controllers

In order to understand controllers easily, I am adding a real-world scenario. It will help you in visualize controllers.

Real World Scenario to Understand Controller

Everyone knows Domino's and Pizza Hut. Let’s take an example of Pizza Hut and understand controllers along with Model and View.

1. You are a customer and you visit the Pizza Hut.
[It means you are a user and visiting ASP.NET MVC Website]

2. There are lots of offers and menus are sticked with the wall and there is also a menu card on the table. You pick menu card and browse the different types of pizza. Finally, you select a pizza and Choco Flavored Coffee and pass an order to the waiter.
[It means you browse website and click on a link]

3. The waiter goes to the purchase counter and passes the list of your order to Manager.
[It is like, you have sent HTTP request to the controller. Here waiter carries your HTTP request and Manager is a controller]

4. Manager, who is standing on the purchase counter, takes the order and pass it to the suitable branch to complete your request and prepare food.
[Here, Manager is a controller that passes the request to model to gather all the information]

5. Next, the Coffee department goes to store room for arranging all the material that will be used in making Choco Flavored Coffee-like, Coffee powder, chocolate powder, milk, cream etc.
[It is like model is asking database for gathering information]

6. Once your order is ready, it is decorated in the tray and manager sends this tray to your table.
[it means, controller, render a ViewPage with all the information that you have requested and serves you.]

 

Theory

In MVC, Controllers controls everything. It is responsible for handle user requests that come from asp.net websites. Learn controller’s job step by step.

mvc flow
1. User made a request through asp.net website.
2. All the requests reach to controllers. Controller asks models for data.
3. The model performs database operation and responds controller with data.
4. Controller gets data from the model.
5. Controller renders HTML page with model data and creates view page.
6. Controller response to the user with View Page.

Some Facts about controllers

1. The controller is a main part of MVC.
2. It is a mediator between client-side request and server-side response.
3. The controller is like a bridge between view page and models.
4. Controller work with HTTP request that comes from the browser.
5. Even, if a user needs a simple static page, the controller comes in action and returns the requested page.
6. Each browser request is mapped with the particular controller and each controller has several action methods to handle browser requests.
7. In MVC, URL doesn’t map with a direct static page. It is mapped with controllers.
8. All controller class must be named by using the “Controller” suffix.

Summary

I hope, now it is clear to you what the controller is and how it works with models and views. It is just a theory part, in which I tried to make you understand basics of controllers and its working mechanism. In the next chapter, you will learn How to Add a Controller and ViewPage in ASP.NET MVC 5 project.

 

Share your thought