Action Methods in ASP.NET MVC

108
action methods in mvc

Action method in MVC

In the previous article, we learned about the controller in mvc and routing in mvc. In this article, we will learn about the action method of the controller class in asp.net MVC.  which in turn created a default HomeController. Here, we will create new StudentController.

All the public methods of a Controller class are called Action methods. They are like any other normal methods with the following restrictions:

  1. The action method must be public. It cannot be private or protected.
  2. Action method cannot be overloaded.
  3. Action method cannot be a static method.

The following is an example of the Index action method of StudentController

Action Method in ASP.NET MVC

As you can see in the above figure, the Index method is a public method and it returns ActionResult using the View() method. The View() method is defined in the Controller base class, which returns the appropriate ActionResult.

Default Action Method

Every controller can have a default action method as per the configured route in RouteConfig class. By default, Index is a default action method for any controller, as per configured default root as shown below.

Default Route:

However, you can change the default action name as per your requirement in RouteConfig class.

ActionResult

MVC framework includes various result classes, which can be returned from an action method. There result classes represent different types of responses such as HTML, file, string, JSON, javascript, etc. The following table lists all the result classes available in ASP.NET MVC.

Result Class Description
ViewResult Represents HTML and markup.
EmptyResult Represents No response.
ContentResult Represents string literal.
FileContentResult/ FilePathResult/ FileStreamResult Represents the content of a file
JavaScriptResult Represent a JavaScript script.
JsonResult Represent JSON that can be used in AJAX
RedirectResult Represents a redirection to a new URL
RedirectToRouteResult Represent another action of same or other controllers
PartialViewResult Returns HTML from Partial view
HttpUnauthorizedResult Returns HTTP 403 status

The ActionResult class is a base class of all the above result classes, so it can be the return type of action methods which returns any type of result listed above. However, you can specify an appropriate result class as a return type of action method.

The Index() method of StudentController in the above figure uses the View() method to return ViewResult (which is derived from ActionResult). The View() method is defined in the base Controller class. It also contains different methods, which automatically returns a particular type of result as shown in the below table.

Result Class Description Base Controller Method
ViewResult Represents HTML and markup. View()
EmptyResult Represents No response.
ContentResult Represents string literal. Content()
FileContentResult,
FilePathResult,
FileStreamResult
Represents the content of a file File()
JavaScriptResult Represent a JavaScript script. JavaScript()
JsonResult Represent JSON that can be used in AJAX Json()
RedirectResult Represents a redirection to a new URL Redirect()
RedirectToRouteResult Represent another action of same or other controller RedirectToRoute()
PartialViewResult Returns HTML PartialView()
HttpUnauthorizedResult Returns HTTP 403 status

As you can see in the above table, the View method returns ViewResult, the Content method returns a string, File method returns the content of a file and so on. Use different methods mentioned in the above table, to return different types of results from an action method.

Action Method Parameters

Every action methods can have input parameters as normal methods. It can be primitive data type or complex type parameters as shown in the below example.

Example: Action method parameters

Note: The action method parameter can be Nullable Type.

By default, the values for action method parameters are retrieved from the request’s data collection. The data collection includes name/values pairs for form data or query string values or cookie values. Model binding in ASP.NET MVC automatically maps the URL query string or form data collection to the action method parameters if both names are matching.

Conclusion

I hope you liked this article based on action methods in mvc. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

LEAVE A REPLY

Please enter your comment!
Please enter your name here