Controller Action Methods

Action methods refer to all of the Controller's public methods. They work in the same way as any other standard method, with the following exceptions:

  1. Action method must be public. It can't be kept private or protected.
  2. Action method cannot be overloaded.
  3. Action method cannot be static method.
The Index() action method in the StudentController class is shown below.

Action_Method

The Index() method is public, as you can see in the above diagram, and it uses the View() method to return the ActionResult. The Controller base class defines the View() method, which returns the required ActionResult.

Default Action Method

According to the configured route in the RouteConfig class, every controller can have a default action method. the Index() method is a default action method for any controller, as shown below.

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}/{name}",
    defaults: new { controller = "Home", 
                    action = "Index", 
                    id = UrlParameter.Optional
            });
However, you can change the default action name in the RouteConfig class to suit your needs.

ActionResult

in the MVC framework Various Result classes are included  which can be returned from action methods. The result classes represent different types of responses, such as HTML, file, string, JSON, javascript, etc. The following table lists all the result classes available.

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

Since the ActionResult class is the base class for all of the above result types, it can be used as the return form of any action method that returns any of the above results. However, as a return type of action process, you can define the required result class.

The Index() method of the StudentController in the above figure uses the View() method to return a ViewResult (which is derived from the ActionResult class) . The base Controller class contains the View() method along with other methods that return a particular type of result, as shown in the below table.


Result Class Description Base Controller Method
ViewResult HTML and markup. View()
EmptyResult No response.
ContentResult string literal. Content()
FileContentResult,
FilePathResult,
FileStreamResult
the content of a file. File()
JavaScriptResult JavaScript script. JavaScript()
JsonResult JSON that can be used in AJAX. Json()
RedirectResult Redirection to a new URL. Redirect()
RedirectToRouteResult Redirection to another route. RedirectToRoute()
PartialViewResult The partial view result. PartialView()
HttpUnauthorizedResult HTTP 403 response.

The View() method returns the ViewResult, the Content() method returns a string, the File() method returns the content of a file, and so on, as shown in the table above. To return a different form of result from an action method, use the methods mentioned in the table above.

Action Method Parameters

As with usual methods, all action methods may have input parameters. it can be primitive data type or complex type parameters, As shown below.
[HttpPost]
public ActionResult Edit(Student std)
{
    // update student record
    
    return RedirectToAction("Index");
}

[HttpDelete]
public ActionResult Delete(int id)
{
    // delete student record whose id matches with specified id passed in parameter

    return RedirectToAction("Index");
}

Please keep in mind that 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. If the URL query string or form data collection names match, model binding in ASP.NET MVC automatically maps the URL query string or form data collection to the action method parameters.

Post a Comment

0 Comments