Part 4 Controller in asp.net mvc
Welcome to the part 4 of ASP.Net MVC video Series, where we are going to look in what is Controllers which is the most talked and the center piece of a MVC Application. They handle all the incoming browser request, load the corresponding View after processing the retrieved data from the Model layer.
So Let us understand what is controller, there job is look for incoming request and then convert it into an output response. So between the request and response they get there data by service layer, which can be a model. The data received is then injected onto the View for rendering on the browser.
Let me take you through the overall picture
1) User request for something by typing URL or clicking link
2) The URL is matched with the pattern of route Engine to call the specific controller
3) Controller fetches data/Checks schema using Model does processing
4) This is then injected into the view, which is the response the user sees on the browser.
Hence in an MVC application controllers handles any incoming URL Request.
So let’s go and create our first controller class.
****Setting up the controller.: Programming*****
Pointers
---------------
Some pointers to note
1) Controller’s class name must end with a word “controller”. For e.g. Our controller Student will be called “StudentController”
2) Controller are class derived from the base class “System.Web.Mvc.Controller”
3) They consist of Public methods called action method. We will discuss what action methods are. So keep watching.
4) A method used as a controller action cannot be overloaded
5) Furthermore, a controller action cannot be a static method.
Let’s look into the journey to controller which is how URL is translated into appropriate controller by the Route Config., for e.g.
if you have a URL like
http://localhost/Student/Index/3
Then based on the URL the controller class “STUDENT CONTROLLER” is called where then the method Index is called. On that Index method then a particular record with ID of 5 is requested.
Based on the Request the Route Engine is looked for the matching pattern and then appropriate controller is called.
Now earlier I told about the RouteConfig and how the URL helps to call the right controller, let see that.
*****Setting up the RouteConfig: Programing****
Points discussed
----------------------------
a) Showing the url with just controller, still calling the Action method.
b) How app start is called under Global.asax folder
c) How the routing works.
Now we saw how controller gets thing, and how they process thing but the one important thing we still have to see is how they responds to thing. Which is what kind of Actions they do.
In ASP.NET, MVC has different types of Action Results. Each action result returns a different format of output. So based on the output you want to send different type of action result are required. Let see the different ActionResult classes
ActionResult class is the base class for all the action results. Now each of these actionresults have helper methods that return the appropriate response
for e.g. The View method returns an instance of the ViewResult class which implement ViewResultBase class, which is derived from ActionResult.
So this View method is used when you want to call the Corresponding view page like in our solution cshtml file and pass the data there.
Another example Redirect Result : where we redirect to another action method by using its URL. The helper method used here is redirect.