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:
- Action method must be public. It can't be kept private or protected.
- Action method cannot be overloaded.
- Action method cannot be static method.
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
| 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. |
Action Method Parameters
[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.

0 Comments