Best approach to handle action methods in MVC.

Jitendrasoft09
Posted by in ASP.NET MVC category on for Intermediate level | Points: 250 | Views : 3887 red flag
Rating: 5 out of 5  
 2 vote(s)

This article helps to understand the various types of action methods in MVC.

Introduction

We would like to understand first what is action method and what is the role of action methods in MVC.  Action is a return type that can have various types. Let us discuss various types of action methods. Actions returns uses in controller to load the request in views. Basically Developers uses the various action results to perform the different types of results in MVC.


Background

Action result inherits the System.Web.Mvc.dll. When we hit the view method then Action result constructor calls to initiate the new instance of Action result class. Action result is the one of the advantage of MVC where we can execute the result with ExecuteResult method and returns the different type results. Probably ASP.NET developer were writing the code to return the types with like int, string, float, char, double etc and based on that he was returning the return type. It was very constant and restricts to return the particular type of the data types. So here Action results come over this picture to handle different types of return in method.

In below figure, we are seeing the differences between the MVC ASP.NET developer and ASP.NET Developer. When we use ActionResult it accepts the any return types. But when we use int, class object or etc then it accepts the particular return types which is used in functions.  


Types of Action Methods

Let us see first the various types of action methods which are used in controller in below figure. Here the below figure shows the various Action methods. The all below action result methods are implemented in controller. As we know when we run the application then first hits the controller and then return the any specified action method on view. We can test the all action method after typing the name of controller and action result method in URL.

Example: http://localhost:1142//Login/Index

Let us discuss how various action methods interact with MVC namespace.  MVC is accessible from System.Web library.  All the actions methods are accessible from System.Web.Mvc.  We can keep this library in top of the controller code using keyword as given in below example.

Example: Using System.Web.Mvc.

ActionResult : This class uses in System.Web.Mvc.ActionResult.

ViewResult : This class uses in System.Web.Mvc.ViewResult.

FileResult : This class uses in System.Web.Mvc.FileResult.

JsonResult : This class uses in System.Web.Mvc.JsonResult.

RedirectResult :  This class uses in System.Web.Mvc.RedirectResult.

JavaScriptResult : This class uses in System.Web.Mvc.JavaScriptResult.

RedirectToRouteResult : This class uses in System.Web.Mvc.RedirectToRouteResult.

HttpStatusCodeResult : This class uses in System.Web.Mvc.HttpStatusCodeResult.

ContentResult : This class uses in System.Web.Mvc.ContentResult.

EmptyResult : This class uses in System.Web.Mvc.EmptyResult


Action Methods Examples

We will discuss more on action methods types with an example and code. Most of the time, developers uses the ActionResult.  Because it does not worry about what data types returns in action result.

Let us see below various types of action methods with example.

ActionResult :  Here Index is called Action method which returns the Index view. Action result accepts the various return types. It encapsulates the results of the Index method and returns on views. Action Result is an abstract class. It is a base class of View Result.

public ActionResult Index()
        {
            return View();
        }

ViewResult :  Here Index is called Action method which returns the Index view. View result accepts the same view return types. It encapsulates the results of the Index method and that is used to render a specified view. View Result is a derived class of Action result class.

public ViewResult Index()
        {
            return View();
        }

FileResult :  Here Index is called Action method which returns the Index view. File result accepts the binary File content return types. It encapsulates the binary file results of the Index method and that is used to send binary file content on view.

public FileResult Index()
        {
            byte[] image = System.IO.File.ReadAllBytes(@"c:\pic.jpg");
            return File(image, "applicaiton/jpeg");
 
        }

JsonResult :  Here Index is called Action method which returns the Index view. JSON result accepts the JSON formatted content return types. It encapsulates the JSON results of the Index method and that is used to send JSON results on view.

public JsonResult Index()

        {
            return Json(true,JsonRequestBehavior.AllowGet);
        }

RedirectResult :  Here Index is called Action method which returns the Index view. Redirect result accepts the specified URL content return types. It encapsulates the URL content results of the Index method and that is used to send URL content results on view.

public RedirectResult Index()
        {
           return Redirect("Login/Welcome");
        }

JavaScriptResult :  Here Index is called Action method which returns the Index view. JavaScript result accepts the JavaScript content return types. It encapsulates the scripts content results of the Index method and that is used to send java script results on view.

public JavaScriptResult Index(Employee employee)
        {
            return JavaScript("<script>alert('Hello!')</script>");
        }

RedirectToRouteResult :  Here Index is called Action method which returns the Index view. RedirectToRoute accepts the route value dictionary. It encapsulates the route value dictionary results of the Index method and that is used to send route value results on view.

public RedirectToRouteResult Index()
        {
            return RedirectToRoute("Hello!");
        }

 

HttpStatusCodeResult :  Here Index is called Action method which returns the Index view. HttpStatusCode result accepts the specific HTTP response status. It encapsulates the HttpStatus code results of the Index method and that is used to send Http Status results on view.

public HttpStatusCodeResult Index()      {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Bad Request");
      }

ContentResult :  Here Index is called Action method which returns the Index view. Content result accepts the user defined content type. It encapsulates the content type results of the Index method and that is used to send content type results on view.

public ContentResult Index()
        {
            return Content("DotNetFunda","html/plain",Encoding.UTF8);
        }

EmptyResult :  Here Index is called Action method which returns the Index view. Empty result accepts nothing in view.  It encapsulates the nothing results in Index method and that is used to send empty results.

public ActionResult Index()
        {
            return new EmptyResult();
        }

Conclusion

Finally, We discussed the various types of action methods with code explanation example in MVC.  As of now we have learned and understood the uses of MVC Action results. Thanks for the reading.


Page copy protected against web site content infringement by Copyscape

About the Author

Jitendrasoft09
Full Name: Jitendra Kumar
Member Level:
Member Status: Member,MVP
Member Since: 3/26/2013 2:40:53 AM
Country: India
Jitendra Kumar If my post helps you, plz mark as an answer.
http://www.dotnetfunda.com/
Jitendra Kumar

Login to vote for this post.

Comments or Responses

Posted by: Sheonarayan on: 5/13/2015 | Points: 25
Nice explanation Jitendra.

Thanks for sharing.
Posted by: Jitendrasoft09 on: 5/13/2015 | Points: 25
Thanks for appreciation Sheo...

Login to post response

Comment using Facebook(Author doesn't get notification)