Action method:

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.

Action Result:

ActionResult is an abstract class defined in System.Web.MVC namespace. Most of the action methods return an object of class which is derived from ActionResult class. It represents the result of MVC action method. In MVC most of the action method returns a View which is an instance of ViewResult class. There result classes represent different types of responses such as html, file, string, json, javascript etc.

Below are different types of ActionResult provided by ASP.NET MVC framework.

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 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.

 

0 Comments1

Sorry, No more post.

Your Comment

Login To Comment

discard

By posting your Comment, you agree to the privacy policy and terms of service.