Action Result Return Types In ASP.Net MVC

What is ActionResult ?
ActionResult is the abstract class which is used to show the output to the client in different format as per result view  returned by the controller.
The ActionResult are defined into the controller and controller returns to the client (Browser).
The following are the sub type or types of Action Results which are present in MVC which are used as per output requirement of client.

  1. ViewResult 
  2. PartialViewResult  
  3. EmptyResult 
  4. RedirectResult 
  5.  RedirectToRouteResult 
  6. JsonResult  
  7. JavaScriptResult
  8. ContentResult 
  9. FileContentResult
  10. FileStreamResult
  11.  FilePathResult 
Let us learn in brief about each ActionResult are as

ViewResult
 It returns the a specified view to the response stream such as html. Syntax
public class EmpController : Controller
{
    
    public ActionResult Index()
    {
        return View();
    }
}
PartialViewResult 
 It returns the partial view to the response stream,mostly the partial view call from main view.Its same like as user control in Asp.net.
Syntax
public class EmpController : Controller
{
    
    public ActionResult Index()
    {
        return PartialView("_Empdetails");
    }
}
EmptyResult  
This action returns the empty response to the client. Syntax 
public class EmpController : Controller
{
    
    public ActionResult Index()
    {
      return new EmptyResult();
    }
}
  • RedirectResult -It redirects to the specified url same as Response.Redirect in Asp.net.
 Syntax
public class EmpController : Controller
{
    
    public ActionResult Index()
    {
     return Redirect("http://www.compilemode.com");
    }
}
  • RedirectToRouteResult - Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data.
Syntax
public class EmpController : Controller
{
    public ActionResult Index()
    {
    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { 
    action = "View", 
    controller = "EmpController",}));
    }
}
JsonResult
This action Serializes a given View Data object into JSON (JavaScript object notation) format. Syntax 
public class EmpController : Controller
{
    public JsonResult EmpDet()  
        {  
     
            return Json("HI", JsonRequestBehavior.AllowGet);  
        }   
   
}
JavaScriptResult 
This action returns a piece of JavaScript code that can be executed on the client side.
Syntax
public class EmpController : Controller
{
public virtual JavaScriptResult EmpDet()
{
    var script = string.Format(@"
    MaxNotificaitonsToShow = {0};
    ItemsPerPage = 5;",
    GlobalSettings.MaxNotificaitonsToShow);
    return JavaScript(script);
}
}
ContentResult 
It writes content to the response stream without requiring a view. Syntax 
public class EmpController : Controller
{
public ContentResult PersonInfo()
{
    return Content("Hi Vithal Wadje");
}
}
FileContentResult 
It returns a file to the client. Syntax 
public class EmpController : Controller
{
public FileResult DailyReport()
{
    string fileName = @"c:\Vithal Wadje\DailyReport.pdf";
    string contentType = "application/pdf";
 
    return new FilePathResult(fileName, contentType);
}
}
FileStreamResult  
This action returns a file to the client, which is provided by a Stream. Syntax
public class EmpController : Controller
{  
public ActionResult DownloadFile()
{
    string Fileinfo =@"c:\Vithal Wadje\DailyReport.txt";
    byte[] data = Encoding.UTF8.GetBytes(Fileinfo );
    return File(data, "text/plain","DailyReport.txt");
}
}
FilePathResult  
This action returns a file to the client.  Syntax 
public class EmpController : Controller
{
public FilePathResult DownloadFile(string file, Guid GuID)
{ 
    return File(FileStream, "application/octet-stream", fileName);
}
} 

Post a Comment

0 Comments