If a user requests an URL then the ASP.NET MVC framework maps URLs to classes that are referred to as Controllers. Controllers process incoming requests using action methods. Action methods typically have a one-to-one mapping with user interactions. When a user enters a URL into the browser, the MVC application uses routing rules that are defined in the Global.asax file to parse the URL and to determine the path of the controller. The controller then determines the appropriate action method to handle the request. As per MSDN, a method must meet the following requirements:
- The method must be public.
- The method cannot be a static method.
- The method cannot be an extension method.
- The method cannot be a constructor,getter, or setter.
- The method cannot have open generic types.
- The method is not a method of the controller base class.
- The method cannot contain ref or out parameters
We can create action methods that return an object of any type, such as a string, an integer, or a Boolean value. These return types are wrapped in an appropriate ActionResult type before they are rendered to the response stream. The ASP.NET MVC framework will convert any return type that is not an action result into a string and render the string to the browser. Create a simple controller as in the following code snippet:
- public class HomeController : Controller
- {
- public ActionResult SayHello()
- {
- ViewData["SayHello"] = "Hello";
- return View();
- }
- public string SayHi()
- {
- return "hi from 'SayHi'.";
- }
- }
We can make a public function a non-action by decorating it with [NonAction] as in the following:
- [NonAction]
- public string SayHi()
- {
- return "hi from 'SayHi'.";
- }
The successful execution of an MVC controller action will produce an object derived from ActionResult. Rendering a view and redirecting the browser to a new URL are both valid types of results we can get from our controller. The complete list of ActionResult derived types that can be an output of action method are as follows.
| Name | Framework Behavior | Producing Method |
| ContentResult | Writes a string value directly into the response. | Content |
| EmptyResult | Blank HTTP response. | |
| FileContentResult | Takes the contents of a file (represented as an array of bytes) and writes the contents into the HTTP response. | File |
| FilePathResult | Takes the contents of a file at the given location and writes the contents into the HTTP response. | File |
| FileStreamResult | Takes a file stream produced by the controller and writes the stream into the HTTP response. | File |
| HttpUnauthorizedResult | A special result used by authorization filters when authorization checks fail. | |
| JavaScriptResult | Responds to the client with a script for the client to execute. | JavaScript |
| JsonResult | Responds to the client with data in JavaScript Object Notation (JSON). | JSON |
| RedirectResult | Redirects the client to a new URL. | Redirect |
| RedirectToRouteResult | Renders the specified view to respond with an HTML fragment (typically used in AJAX scenarios). | RedirectToRoute / RedirectToAction |
| PartialViewResult | Renders the specified view to respond with an HTML fragment (typically used in AJAX scenarios). | PartialView |
| ViewResult | Renders the specified view and responds to the client with HTML. | View |



Sudheshwer RaiPosted Sep 30, 2018, 9:56 AM
Nice article brother.
SubashPosted Sep 9, 2016, 10:49 AM
Nice explanation
Gowtham RajamanickamPosted Aug 27, 2015, 9:50 AM
good
Yashwanth MuthineniPosted Aug 27, 2015, 3:23 AM
Nice Share
Ankit BansalPosted Aug 27, 2015, 2:22 AM
Good one..Thanks for sharing..
Rajeesh MenothPosted Aug 27, 2015, 1:54 AM
Good One
Mohammed IbrahimPosted Aug 27, 2015, 12:07 AM
Nice
Jaipal ReddyPosted Aug 26, 2015, 11:45 PM
Good one sir.
Debasis SahaPosted Aug 26, 2015, 10:17 AM
Good One..
Karthikeyan KPosted Aug 26, 2015, 8:04 AM
Good one sir...Thanks for share