Introduction
In ASP.NET MVC, controller action methods return different types of results depending on what the application needs to send back to the browser.
For example, an action method may return:
A View page
JSON data
A file download
Redirect to another page
These responses are called Action Results.
Understanding return types helps developers choose the correct response type when building web applications.
In this article, we will learn:
What return types are in ASP.NET MVC
Why they are used
Different return types with examples
When to use each return type
What is ActionResult?
ActionResult is the base class for all result types in ASP.NET MVC.
It allows a controller action method to return different types of responses.
Example:
public ActionResult Index()
{
return View();
}
Here the controller returns a ViewResult, but the return type is ActionResult.
This gives flexibility to return different results.
1️⃣ ViewResult
What is ViewResult?
ViewResult returns an HTML view page to the browser.
Example
Controller:
public ViewResult Index()
{
return View();
}
View:
<h2>Welcome to ASP.NET MVC</h2>
What is this?
Output
Welcome to ASP.NET MVC
When to Use
Use ViewResult when you want to display a web page to the user.
2️⃣ PartialViewResult
What is PartialViewResult?
PartialViewResult returns a partial view instead of a full page.
Partial views are reusable UI components.
Example
public PartialViewResult Header()
{
return PartialView("_Header");
}
When to Use
Use when loading small UI sections like:
Header
Footer
Sidebar
AJAX partial content
3️⃣ JsonResult
What is JsonResult?
JsonResult returns JSON data instead of HTML.
This is commonly used in AJAX calls.
Example
public JsonResult GetStudent()
{
var student = new
{
Id = 1,
Name = "Rahul",
Age = 20
};
return Json(student, JsonRequestBehavior.AllowGet);
}
Output
{
"Id":1,
"Name":"Rahul",
"Age":20
}
When to Use
Use JsonResult when sending data to:
AJAX
JavaScript
APIs
4️⃣ ContentResult
What is ContentResult?
ContentResult returns plain text or HTML content.
Example
public ContentResult Message()
{
return Content("Hello from ASP.NET MVC");
}
Output
Hello from ASP.NET MVC
When to Use
Use when returning simple text content.
5️⃣ FileResult
What is FileResult?
FileResult returns a file download to the browser.
Example
public FileResult Download()
{
string path = Server.MapPath("~/Files/sample.pdf");
return File(path, "application/pdf", "sample.pdf");
}
Output
The browser downloads sample.pdf.
When to Use
Use when allowing users to download:
PDF files
Excel files
Images
Documents
Join the conversation! Your thoughts help the community grow.