DEV Community

Cover image for Mastering in these 10+ Result/Return types in ASP.Net Core MVC
DotNet Full Stack Dev
DotNet Full Stack Dev

Posted on

Mastering in these 10+ Result/Return types in ASP.Net Core MVC

In ASP.NET Core MVC, an action method can return several types of results, known as Action Results. These results determine what type of response the controller sends back to the client. The IActionResult interface defines a standard way to return responses from a controller action method. Let's explore the different result types.

Common Action Results

  1. ViewResult
  2. JsonResult
  3. ContentResult
  4. FileResult
  5. RedirectResult
  6. RedirectToActionResult
  7. RedirectToRouteResult
  8. StatusCodeResult
  9. EmptyResult
  10. PartialViewResult
  11. ObjectResult

1. ViewResult

This result is used to render a view (HTML page) to the client. It is the most common type used in MVC applications.

Example

public IActionResult Index()
{
    return View();
}
Enter fullscreen mode Exit fullscreen mode

2. JsonResult

This result is used to return JSON-formatted data. It is commonly used for API responses.

Example

public JsonResult GetJsonData()
{
    var data = new { Name = "John", Age = 30 };
    return Json(data);
}
Enter fullscreen mode Exit fullscreen mode

3. ContentResult

This result is used to return plain text or any other content.

Example

public ContentResult GetContent()
{
    return Content("Hello, this is plain text.");
}
Enter fullscreen mode Exit fullscreen mode

4. FileResult

This result is used to return a file to the client, such as a document, image, or any other file.

Example

public FileResult GetFile()
{
    var fileBytes = System.IO.File.ReadAllBytes("path/to/file.pdf");
    return File(fileBytes, "application/pdf", "download.pdf");
}
Enter fullscreen mode Exit fullscreen mode

5. RedirectResult

This result is used to redirect the client to a specified URL.

Example

public RedirectResult RedirectToUrl()
{
    return Redirect("https://www.example.com");
}
Enter fullscreen mode Exit fullscreen mode

6. RedirectToActionResult

This result is used to redirect the client to a specific action method in the controller.

Example

public RedirectToActionResult RedirectToActionMethod()
{
    return RedirectToAction("Index", "Home");
}
Enter fullscreen mode Exit fullscreen mode

7. RedirectToRouteResult

This result is used to redirect the client to a specific route.

Example

public RedirectToRouteResult RedirectToRouteMethod()
{
    return RedirectToRoute(new { controller = "Home", action = "Index" });
}
Enter fullscreen mode Exit fullscreen mode

8. StatusCodeResult

This result is used to return a specific HTTP status code.

Example

public StatusCodeResult GetStatusCode()
{
    return StatusCode(404);
}
Enter fullscreen mode Exit fullscreen mode

9. EmptyResult

This result does not return any content.

Example

public EmptyResult DoNothing()
{
    return new EmptyResult();
}
Enter fullscreen mode Exit fullscreen mode

10. PartialViewResult

This result is used to render a partial view.

Example

public PartialViewResult GetPartialView()
{
    return PartialView("_PartialView");
}
Enter fullscreen mode Exit fullscreen mode

11. ObjectResult

This result is used to return an object as the response. It is often used in APIs to return data along with an HTTP status code.

Example

public ObjectResult GetObject()
{
    var data = new { Name = "John", Age = 30 };
    return new ObjectResult(data) { StatusCode = 200 };
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

ASP.NET Core MVC provides a rich set of action results that allow you to return various types of responses from your controller actions. Each result type serves a specific purpose and can be used to meet the requirements of different scenarios. Understanding these result types is crucial for building robust and flexible web applications.

With these action results, you can efficiently manage how your application responds to client requests, whether it's rendering views, returning JSON data, or handling file downloads.

Top comments (0)