Basic return types of ActionResults in ASP.NET MVC

This is very important topic in asp.net mvc.Here you will learn about different types of action results returned by action methods in the MVC controller. In MVC Controller file you have many action methods.Each action method can return different return types of results like contentresult,javascript,json or view. Actionresult is an abstract class and it is the base class for all types of action results like ViewResult,PartialViewResult,JsonResult,ContentResult,
JavascriptResult,RedirectResult,RedirectToRouteResult,FileResult and EmptyResult.
The following diagram shows return types of actionresults in ASP.NET MVC:-


The code below shows the 2 methods of the Abstract class ActionResult.
public abstract class Actionresult
{
    protected actionresult(){}
    public abstract void ExecuteResult(ControllerContext context){}
}
        

It can be easily remembered by seperating the results under a seperate category for each results.


As you can see the following 7 types of content returning results.We will first see Content Returning Results one by one.

Basic return types of actionresults in ASP.NET MVC are :-
  • ViewResult
  • PartialViewResult
  • Contentresult
  • Emptyresult
  • Fileresult
  • Json result
  • Javascript result

ViewResult

If you want to return a view in action method , you should use View as return type of that method. See the following example,


public class EmployeeController : Controller
{                  
public ViewResult Index()
{
return View();
}
}
  
Output:

PartialViewResult

If you want to return partialview in action method,you should use partialviewresult as return type of that method. Partialview is used to call inside our Normal view page.For eg:-if i need to call left menu bar inside my normal view page.Let us take this example itself.Suppose if i need to call left side ASP.NET MVC 5 TUTORIALS menu bar inside my ActionReturntypes page then i have to use this partialview concept. We have to create partial view inside shared folder.Let us see this by example, First create partial view page in shared folder.


PartialViewexample.cshtml
<div style="border:1px solid blue;height:400px;width:300px;">
This is my partial view
<div>
MainPage.cshtml
 @Html.Partial("partialViewExample");
 <div style="margin-top:-10px">
 <h2>This is the output of my MAIN PAGE. <h2>
 <div>
Employeecontroller.cs
public class EmployeeController : Controller
{
    // GET: Employee
    public ActionResult Index()
    {
         return View();
    }
}
Output:

ContentResult

If you want to return your content to the view then you should use Content as the return type of the action method. you can return htmlresult,javascript result etc.

 public class EmployeeController : Controller
 {
// GET: Employee
public ContentResult Index()
{
  return Content("<h1><font color='blue'>I AM HAPPY WITH<font>, WHAT I HAVE<h1><script>alert('HI USMTECHWORLD , HOW R U?');<script>");
}
}
Output:

EmptyResult

This emptyresult returns nothing in the view page.By seeing its code snippet you can very well understand about this,

public class EmployeeController : Controller
{
 // GET: Employee
public EmptyResult Index()
{
 return new EmptyResult(); or return null;
}
}
Output:

FileResult

If you want to return file to the view then you should use File as the return type of the action method.The FileREsult class has File method inside and has 2 parameters i.e Filename and ContentType.

public class EmployeeController : Controller
{
// GET: Employee
public FileResult Index()
{
return File("~/Aboutme.txt", "text/plain");
}
}
Output:
Suppose if you dont want to show your file in the browser, but asked to download it then you have to do some little alteration in the exising code.Just give filedownloadname as 3rd parameter in File return type.
 public class EmployeeController : Controller
 {
 // GET: Employee
 public FileResult Index()
 {
 return File("~/Aboutme.txt", "text/plain","AboutmeDownload.txt");
 }
 }
Output:

JSONResult

If you want to return JSON data to the view then you should use JSON as the return type of the action method.

 public class EmployeeController : Controller
 {
// GET: Employee
    public JsonResult Index()
    {
        return Json(new {WEBSITE="WWW.USMTECHWORLD.COM",NOOFVISITORS="2,00,000" });
    }
}
If you run the above code, you will receive block request error.This is beacause your sensitive information will be disclosed to the third party websites.So to fix this error you should allow get request using JSONREQUESTBEHAVIOUR.ALLOWGET. Output:

Fix for Above Error:

public class EmployeeController : Controller
{
// GET: Employee
public JsonResult Index()
{
    return Json(new {WEBSITE="WWW.USMTECHWORLD.COM",NOOFVISITORS="2,00,000" },JsonRequestBehavior.AllowGet);
}
}
Output:

JavascriptResult

If you want to return javascript to the view then you should use JavaScript as the return type of the action method.

Employeecontroller.cs
public JavaScriptResult CallJavascriptFromControllerActionAlert()
{
return JavaScript("alert('HELLO USMTECHWORLD')");
}
Index.html:=
            
@Html.ActionLink("Call Javascript From Controller Action", "CallJavascriptFromControllerActionAlert")
Output: