ASP.NET MVC application uses routing rules defined in Global.asax to find out the appropriate Controller and pass the request to it. The Controller receives the request sent by the client and further calls appropriate Model that interact with database and fetches data back to Controller. Controller calls respective View and pass data which is further rendered to client as response. For more details about MVC Routing, please follow ASP.NET MVC Tutorial here.
Below is the pictorial view of above discussion.
After examining the above diagram, we can conclude that a Controller in MVC is performing the following tasks:
- Receiving requests coming from client
- Demanding the model for data or updates on data
- Providing data to appropriate view for rendering response
By performing above mentioned tasks, a controller basically facilitates to achieve loose coupling between model and View which is highly desirable for Object Oriented Systems.
If we dive deeper to understand about controller, it’s basically a class inheriting from System.Web.Mvc.Controller and placed under Controllers folder. A public method inside a controller class is considered to be a Controller’s Action Method. Each client request reaching Controller is for a specific Action Method. A Controller class can have multiple action methods that are responsible for performing certain operations depending upon the client action. Please look into code for a very simple controller i.e. ProductController.
Every Controller should have at least one action method. Here Index() is the default action method for ProductController controller. It will be invoked if no action is provided explicitly by the client.
Note: As discussed above that a public method inside a controller is considered to be an Action Method. But if we explicitly don’t want a public method to be an Action Method, we will mark it as Non Action attribute as:
public void SomeNonActionMethod()
More on ASP.NET MVC Action Methods:
In above code, ProductController has only one default Action Method. In order to fulfill different client requests, our ProductController class can have more Action Methods for all desired user actions as:
- Index() – To get all Products list.
- Details(int? Id) – To get a specific Product details.
- Create() – Add a new Product.
- Edit(int? Id) – Update an existing Product.
- Delete(int? Id) – Delete an existing Product.
Calling Pattern of a Controller’s Action Method is as:
For Example, client can call our ProductController’s Action Methods as:
- Index() as:
or simply http://localhost:XXXX/Product/
- Details(string Id) as:
- Create() as:
- Edit(string Id) as:
- Delete(string Id) as:
All these above action methods can be invoked by using any HTTP request method (like GET, POST, PUT, DELETE). By default, ASP.NET MVC framework does not apply any restriction. But we can restrict our action method to be invoked by a specific HTTP request method. We can apply such restriction by using HTTP request method attributes as HttpGet, HttpPost, HttpPut and HttpDelete.
public ActionResult Index()...
public ActionResult Details(int? Id)...
public ActionResult Create()...
public ActionResult Edit(int? Id)...
public ActionResult Delete(int? Id)...
How to change an Action Method’s Name?
In ASP.NET MVC, we can use ActionName attribute to give a changed name to our action methods. Caller(the client) will be using the name given in ActionName attribute as:
public ActionResult Index()
Now, the Index() action method will be called by name “GetAllProducts”.
Action Results and Helper Methods
All above Action methods perform certain operations by interacting with Model and return output data back to View. Return type of an action method in ASP.NET MVC is called an action result. ASP.NET MVC framework provides an abstract class named “ActionResult” which is basically a base class for all action results. Also, there are many built-in action result types derived from ActionResult class like ViewResult, PartialViewResult, RedirectResult etc.
Following code snippet clearly shows that Index() action method has a return type ActionResult and View() as Helper Method.
Return type can be any of the derived type but it all depends what task an action method is performing. For example,
- In case of redirect to another action method, Helper method will be Redirect() and action result will be RedirectResult(derived class of ActionResult).
public RedirectResult Redirect()
- Similarly, to return a JSON object, Helper method will be Json() and action result will be JsonResult (derived class of ActionResult).
public JsonResult GetJsonData(List<Product> product)
return Json(data: product.Description.ToString());
Following is a list of other important Action Result Types with Helper Methods:
|ViewResult||View||It renders a view as a web page|
|PartialViewResult||PartialView||It renders a PartialView.|
|RedirectToRouteResult||RedirectToAction or RedirectToRoute||It redirects to another action method.|
|FileResult||File||It returns a binary output to for response.|
|ContentResult||Content||To return user-defined content type.|
Hopefully, it will be easy to implement ASP.NET MVC application. So, let’s create your first ASP.NET MVC 5 application with Entity Framework step by step.
Other Related Articles:
- Webforms Vs MVC
- ASP.NET MVC3 Vs MVC4 Vs MVC5
- ASP.NET Web API by Example
- jQuery REST call
- WCF Vs Web Services
- Essential WCF Tutorial
- WCF Hosting (Console | Windows Service | IIS)
Top 10 Interview Questions and Answers Series:
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions
- Top 10 HTML5 Interview Questions
- Top 10 ASP.NET Interview Questions
- Comprehensive Series of ASP.NET Interview Questions
- Top 10 ASP.NET MVC Interview Questions
- Top 10 ASP.NET Web API Interview Questions
- Top 10 ASP.NET AJAX Interview Questions