All about ASP.NET MVC Controllers and Action Methods

By | November 5, 2014
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest

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.

ASP.NET MVC

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.Controller in ASP.NET MVC
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:

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:MVC Action Methods

  • 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:
http://Server:Port/{Controller}/{Action}/{Id}

For Example, client can call our ProductController’s Action Methods as:

  • Index()  as:
    http://localhost:XXXX/Product/Index
    or simply http://localhost:XXXX/Product/
  • Details(string Id) as:
    http://localhost:XXXX/Product/Details/5
  • Create() as:
    http://localhost:XXXX/Product/Create
  • Edit(string Id) as:
    http://localhost:XXXX/ProductController/Edit/5
  • Delete(string Id) as:
    http://localhost:XXXX/ProductController/Delete/5

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.

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:

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.
Action Results & Helper Methods
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()
    {
         return Redirect(“AboutMe”);
    }

     

  • 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[0].Description.ToString());
    }

Following is a list of other important Action Result Types with Helper Methods:

Action Result

Helper Method

Short Description

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.
JavaScriptResult JavaScript It returns a script that can be executed on client.
FileResult File It returns a binary output to for response.
ContentResult Content To return user-defined content type.
Please follow here for a complete list of all ActionResult types with respective Helper methods.
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:

ASP.NET Online Test

Top 10 Interview Questions and Answers Series:

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest
Category: ASP.NET MVC

About IMRAN ABDUL GHANI

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.