i

ASP.Net A Complete Guide

Action Methods in MVC

Action methods are the same as the method which we use in .net application. These methods are written in the controller class. This method returns the action result. The base class of all the action methods is the "ActionResult" class.

As per MSDN, a method must meet the following requirements:

  • The Action method must be public.

  • The Action method cannot be a static method.

  • The Action method cannot be an extension method.

  • The Action method cannot be a constructor.

  • The Action method cannot have open generic types.

  • The Action method cannot contain ref or out parameters

Action method return below type of result:

Name

Framework Behavior

RedirectToRouteResult

It will return the specified view. It is from the same controller or another controller.

PartialViewResult

It will return HTML from the partial view.

RedirectResult

It provide URL for redirection.

ViewResult

It will return the HTML and Markup result.

ContentResult

It will write the string on the output stream.

EmptyResult

It will return the empty response.

FileContentResult

It will return the file content on the HTTP response.

FilePathResult

It read the file from the given location and writes the contents to the HTTP response.

FileStreamResult

Takes a file stream produced by the controller action and writes the stream into the HTTP response.

HttpUnauthorizedResult

It returns 403 when authorization fails.

JavaScriptResult

It returns the script result.

 

Example of Action Result:

[HttpPost]

public ActionResult Index()

{   

    return view();

}