Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • ASP.NET MVC - Get Started
  • MVC Architecture
  • MVC Version History
  • Create First MVC App
  • MVC Folder Structure
  • Routing
  • Controller
  • Action method
  • Action Selectors
  • ActionVerbs
  • Model
  • View
  • Integrate Model, View & Controller
  • Model Binding
  • Create Edit View
  • Razor Syntax
  • Html Helpers
  • Exception Handling
  • Validation
  • Layout View
  • Partial View
  • ViewBag
  • ViewData
  • TempData
  • Filters
  • ActionFilters
  • Bundling
  • ScriptBundle
  • StyleBundle
  • Area
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Action method

In this section, you will learn about the action method of the controller class.

All the public methods of the 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.

The following illustrates the Index() action method in the StudentController class.

Action Method

As you can see in the above figure, the Index() method is public, and it returns the 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 the RouteConfig class. By default, the Index() method is a default action method for any controller, as per configured default root, as shown below.

Default Route
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 as per your requirement in the RouteConfig class.

ActionResult

MVC framework includes various Result classes, which can be returned from an action method. 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 in ASP.NET MVC.

Result ClassDescription
ViewResultRepresents HTML and markup.
EmptyResultRepresents No response.
ContentResultRepresents string literal.
FileContentResult/ FilePathResult/ FileStreamResultRepresents the content of a file.
JavaScriptResultRepresent a JavaScript script.
JsonResultRepresent JSON that can be used in AJAX.
RedirectResultRepresents a redirection to a new URL.
RedirectToRouteResultRepresent another action of same or other controller.
PartialViewResultReturns HTML from Partial view.
HttpUnauthorizedResultReturns 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 method that returns any result listed above. However, you can specify the appropriate result class as a return type of action method.

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 includes the View() method along with other methods that return a particular type of result, as shown in the below table.

Result ClassDescriptionBase Controller Method
ViewResultRepresents HTML and markup.View()
EmptyResultRepresents No response.
ContentResultRepresents string literal.Content()
FileContentResult,
FilePathResult,
FileStreamResult
Represents the content of a file.File()
JavaScriptResultRepresents a JavaScript script.JavaScript()
JsonResultRepresents JSON that can be used in AJAX.Json()
RedirectResultRepresents a redirection to a new URL.Redirect()
RedirectToRouteResultRepresents redirection to another route.RedirectToRoute()
PartialViewResultRepresents the partial view result.PartialView()
HttpUnauthorizedResultRepresents HTTP 403 response.

As you can see in the above table, the View() method returns the ViewResult, the Content() method returns a string, the File() method returns the content of a file, and so on. Use different methods mentioned in the above table to return a different type of result 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 below.

Example: Action Method Parameters
[HttpPost]
public ActionResult Edit(Student std)
{
    // update student to the database
    
    return RedirectToAction("Index");
}

[HttpDelete]
public ActionResult Delete(int id)
{
    // delete student from the database whose id matches with specified id

    return RedirectToAction("Index");
}

Please note that action method paramter 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 match. Visit model binding section for more information on it.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.