ASP.NET MVC Action Methods are responsible to execute request and generate response to it. By default it generates response in form of ActionResult. All the public methods of a Controller class are called Action methods.
They are like any other normal methods with the following restrictions:
1. 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.
Default Action method:
Every controller can have default action method as per configured route in RouteConfig class. By default, Index is a default action method for any controller.
Below are different types of ActionResult provided by ASP.NET MVC framework.
|ViewResult||Represents HTML and markup.|
|EmptyResult||Represents No response.|
|ContentResult||Represents string literal.|
|FileContentResult/ FilePathResult/ FileStreamResult||Represents the content of a file|
|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 controller|
|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 return type of action methods which returns any type of result listed above. However, you can specify appropriate result class as a return type of 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. 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.
Action method parameters can be Nullable type.