ActionResults in ASP.Net MVC

 Background

In many forum posts I have read one common question. That is, what are ActionResults in MVC. So this article explains ActionResults of MVC that shows the output to the client. So instead of going into depth on the subject, let us start with an overview.

ActionResult

ActionResult is the abstract class for showing the output to the client in various formats as in the result view returned by the controller. The ActionResult is defined in the controller and the controller returns to the client (the browser).

The following are the subtype or types of Action Results that are present in MVC that are used depending on the output requirement of the client.
  1. ViewResult 
  2. PartialViewResult  
  3. EmptyResult 
  4. RedirectResult 
  5.  RedirectToRouteResult 
  6. JsonResult  
  7. JavaScriptResult
  8. ContentResult 
  9. FileContentResult
  10. FileStreamResult
  11.  FilePathResult
The following is a brief explanation of each ActionResult.
  • ViewResult: returns the view to the client as HTML.
The following is the syntax to define the ViewResult in a MVC controller.

Syntax
  1. public class EmpController : Controller   
  2. {          
  3.   public ActionResult Index()     
  4.   {         
  5.   return View();     
  6.   }   
  7. }  
  • PartialViewResult: returns the partial view to the response stream, usually the partial view called from the main view. It's the same as a user control in ASP.Net.
Syntax  
  1. public class EmpController : Controller  
  2. {  
  3.       
  4.     public ActionResult Index()  
  5.     {  
  6.         return PartialView("_Empdetails");  
  7.     }  

  • EmptyResult: this action returns the empty response to the client.

    Syntax
  1. public class EmpController : Controller  
  2. {  
  3.     public ActionResult Index()  
  4.     {  
  5.       return new EmptyResult();  
  6.     }  

  • RedirectResult: redirects to the specified URL, the same as Response.Redirect in ASP.Net.

    Syntax
  1. public class EmpController : Controller  
  2. {    
  3.     public ActionResult Index()  
  4.     {  
  5.      return Redirect("http://www.compilemode.com");  
  6.     }  

  • RedirectToRouteResult: does an HTTP redirection to a URL that is determined by the routing engine, based on given route data.
Syntax  
  1. public class EmpController : Controller  
  2. {  
  3.     public ActionResult Index()  
  4.     {  
  5.     filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new {   
  6.     action = "View",   
  7.     controller = "EmpController",}));  
  8.     }  

  • JsonResult: serializes a given ViewData object into JavaScript Object Notation (JSON) format.
 
Syntax 
  1. public class EmpController : Controller  
  2. {  
  3.     public JsonResult EmpDet()    
  4.         {        
  5.             return Json("HI", JsonRequestBehavior.AllowGet);    
  6.         }     

  • JavaScriptResult: returns a piece of JavaScript code that can be executed on the client side.
Syntax  
  1. public class EmpController : Controller  
  2. {  
  3. public virtual JavaScriptResult EmpDet()  
  4. {  
  5.     var script = string.Format(@"  
  6.     MaxNotificaitonsToShow = {0};  
  7.     ItemsPerPage = 5;",  
  8.     GlobalSettings.MaxNotificaitonsToShow);  
  9.     return JavaScript(script);  
  10. }  

  • ContentResult: writes the content to the response stream without requiring a view.

Syntax
  1. public class EmpController : Controller  
  2. {  
  3. public ContentResult PersonInfo()  
  4. {  
  5.     return Content("Hi Vithal Wadje");  
  6. }  

  • FileContentResult: This action returns a file to the client.
Syntax
  1. public class EmpController : Controller  
  2. {  
  3. public FileResult DailyReport()  
  4. {  
  5.     string fileName = @"c:\Vithal Wadje\DailyReport.pdf";  
  6.     string contentType = "application/pdf";  
  7.    
  8.     return new FilePathResult(fileName, contentType);  
  9. }  

  • FileStreamResult: returns a file to the client that is provided by a Stream.
Syntax
  1. public class EmpController : Controller  
  2. {    
  3. public ActionResult DownloadFile()  
  4. {  
  5.     string Fileinfo =@"c:\Vithal Wadje\DailyReport.txt";  
  6.     byte[] data = Encoding.UTF8.GetBytes(Fileinfo );  
  7.     return File(data, "text/plain","DailyReport.txt");  
  8. }  

  • FilePathResult: returns a file result to the client. 
Syntax
  1. public class EmpController : Controller  
  2. {  
  3. public FilePathResult DownloadFile(string file, Guid GuID)  
  4. {   
  5.     return File(FileStream, "application/octet-stream", fileName);  
  6. }  
  7. }  
Note

This article is only a brief description of ActionResults so beginners can understand. For more details please read my future article. 

Summary

My next articles will go into depth about each ActionResult. I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.

Up Next
    Ebook Download
    View all
    Learn
    View all