Result 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();
}

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);
}

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.");
}

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");
}

5. RedirectResult

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

Example

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

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");
}

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" });
}

8. StatusCodeResult

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

Example

public StatusCodeResult GetStatusCode()
{
    return StatusCode(404);
}

9. EmptyResult

This result does not return any content.

Example

public EmptyResult DoNothing()
{
    return new EmptyResult();
}

10. PartialViewResult

This result is used to render a partial view.

Example

public PartialViewResult GetPartialView()
{
    return PartialView("_PartialView");
}

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 };
}

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.