Introduction
In ASP.NET MVC, every controller action method returns a result to the browser. This result tells the MVC framework what response should be sent to the user.
For example:
Show a View page
Return JSON data
Redirect to another page
Return plain text
Return a file download
To handle these situations, ASP.NET MVC provides different Action Result return types.
Understanding when to use each return type is very important for building MVC applications.
What is ActionResult?
ActionResult is the base class for many result types in MVC.
Example:
public ActionResult Index()
{
return View();
}
Here the action method returns a ViewResult, but the return type is written as ActionResult.
Why use ActionResult?
Because it allows the method to return different types of results.
Example:
public ActionResult Test()
{
if(true)
return View();
else
return RedirectToAction("Index");
}
Types of Return Types in MVC
Below are the most commonly used return types.
| Return Type | Purpose |
|---|
| ViewResult | Returns a View page |
| PartialViewResult | Returns Partial View |
| ContentResult | Returns plain text |
| JsonResult | Returns JSON data |
| RedirectResult | Redirects to another URL |
| RedirectToRouteResult | Redirects to another action |
| FileResult | Returns a file |
| EmptyResult | Returns nothing |
1 ViewResult
ViewResult is used to display a View page.
Example Controller:
public ViewResult Index()
{
return View();
}
This will open:
Views/Home/Index.cshtml
Passing Data to View
public ActionResult Index()
{
ViewBag.Message = "Welcome to MVC";
return View();
}
View:
<h2>@ViewBag.Message</h2>
What is this?
Output:
Welcome to MVC
2 PartialViewResult
Used to return partial views.
Partial views are small reusable UI components.
Example:
Controller
public PartialViewResult StudentList()
{
return PartialView();
}
Partial View file:
_StudentList.cshtml
When to Use
AJAX requests
Reusable UI sections
Updating part of a page
3 ContentResult
Used to return simple text content.
Example:
public ContentResult Message()
{
return Content("Hello MVC Developers");
}
Output in browser:
Hello MVC Developers
When to Use
Simple text response
Testing APIs
Debugging
4 JsonResult
Used to return JSON data.
Mostly used in AJAX calls.
Example:
public JsonResult GetStudent()
{
var student = new
{
Id = 1,
Name = "Abhay",
City = "Ahmedabad"
};
return Json(student, JsonRequestBehavior.AllowGet);
}
Output
{
"Id":1,
"Name":"Abhay",
"City":"Ahmedabad"
}
When to Use
AJAX requests
API responses
JavaScript data handling
5 RedirectResult
Used to redirect to another URL.
Example:
public RedirectResult GoToGoogle()
{
return Redirect("https://www.google.com");
}
Browser will open Google.
6 RedirectToRouteResult
Used to redirect to another controller action.
Example:
public ActionResult RedirectExample()
{
return RedirectToAction("Index", "Home");
}
This will redirect to:
HomeController → Index Action
7 FileResult
Used to return files to download.
Example:
public FileResult DownloadFile()
{
return File("~/Files/sample.pdf", "application/pdf");
}
The browser will download the file.
When to Use
PDF download
Excel download
Image download
8 EmptyResult
Used when no response is required.
Example:
public EmptyResult Test()
{
return new EmptyResult();
}
This returns nothing to the browser.
Returning Different Data Types to View
In MVC we can send many types of data to views.
1 Using ViewBag
Controller
public ActionResult Index()
{
ViewBag.Name = "Abhay";
ViewBag.Age = 22;
return View();
}
View
<h2>@ViewBag.Name</h2>
<h2>@ViewBag.Age</h2>
What is this?
2 Using ViewData
Controller
public ActionResult Index()
{
ViewData["City"] = "Ahmedabad";
return View();
}
View
<h2>@ViewData["City"]</h2>
What is this?
3 Passing Model Data
Model
public class Student
{
public int Id {get;set;}
public string Name {get;set;}
}
Controller
public ActionResult Details()
{
Student s = new Student();
s.Id = 1;
s.Name = "Abhay";
return View(s);
}
View
@model Student
<h2>@Model.Id</h2>
<h2>@Model.Name</h2>
When to Use Which Return Type
| Situation | Return Type |
|---|
| Display page | ViewResult |
| Load part of page | PartialViewResult |
| Return plain text | ContentResult |
| Return JSON for AJAX | JsonResult |
| Redirect to another action | RedirectToRouteResult |
| Redirect to URL | RedirectResult |
| Download file | FileResult |
| No output needed | EmptyResult |
Real Example (Multiple Return Types)
public ActionResult Example()
{
if(DateTime.Now.Hour < 12)
{
return View();
}
else
{
return RedirectToAction("Index");
}
}
Here one action method can return two different results.
Important Tip for Beginners
Most developers simply use:
public ActionResult Index()
Instead of writing:
public ViewResult Index()
Why?
Because ActionResult allows multiple result types in the same method.
Conclusion
Return types are a fundamental concept in ASP.NET MVC controllers. They define what kind of response should be sent back to the browser.
Using the correct return type improves:
Application structure
Performance
Maintainability
The most commonly used return types are ViewResult, JsonResult, RedirectResult, and FileResult.
Once beginners understand these return types, building MVC applications becomes much easier.