9 return types that we can use to return results from the controller to view. 

ViewResult: The base type of all these result types is ActionResult. ViewResult (View) This return type is used to return a webpage from an action method. 

public ActionResult Index()  {
    // it looks for a view named "Index" in the "Views/Home" folder
    return View();
}

PartialviewResult (Partialview) This return type is used to send a part of a view that will be rendered in another view. 

public ActionResult UserInfo() {
    var user = new { Name = "Alice", Age = 30 };
    return PartialView("_UserInfoPartial", user); /

    // Renders "_UserInfoPartial.cshtml"
}

 

RedirectResult (Redirect) This return type is used to redirect to any other controller and action method depending on the URL. 

public ActionResult GoToGoogle() {
    return Redirect("https://www.google.com");
}

 

RedirectToActionResult (RedirectToAction, RedirectToRoute) This return type is used when we want to redirect to any other action method with in same controller

public ActionResult ProcessOrder(int orderId) {
    // ... process the order ...
    return RedirectToAction("OrderConfirmation", new { id = orderId });
}

public ActionResult OrderConfirmation(int id) {
    // ... display order confirmation ...
    return View();
}

 

RedirectToRouteResult (RedirectToAction, RedirectToRoute) This return type is used when we want to redirect to any other action method of different controller.

public class ProductController : Controller{
   public ActionResult Details(int id)   {
       // ... fetch product details ...
       return View();
   }
}

public class HomeController : Controller{
   public ActionResult ViewProduct(int productId)   {
       return RedirectToRoute(new { controller = "Product", action = "Details", id = productId });
   }
}

 

ContentResult (Content) This return type is used to return HTTP content type like text/plain as the result of the action. 

public ActionResult GetMessage(){
    return Content("Hello from the server!", "text/plain");
}

 

jsonResult (json) This return type is used when we want to return a JSON message.

public ActionResult GetUserData() {
    var user = new { Name = "Bob", City = "New York" };
    return Json(user, JsonRequestBehavior.AllowGet); // Important for GET requests
}

 javascriptResult (javascript) This return type is used to return JavaScript code that will run in the browser. 

public ActionResult AlertMessage() {
    return JavaScript("alert('This is a JavaScript alert!');");
}

 

FileResult (File) This return type is used to send binary output in response. - for in-memory byte array

public ActionResult GetImage(){
    byte[] imageBytes = System.IO.File.ReadAllBytes("path/to/your/image.jpg"); // Replace with actual path
    return File(imageBytes, "image/jpeg");
}

OR

Return for a file on disk

public ActionResult DownloadDocument() {
    string filePath = "path/to/your/document.pdf"; // Replace with actual path
    return File(filePath, "application/pdf", "document.pdf"); // Suggests a filename for download
}

 

EmptyResult This return type is used to return nothing (void) in the result.

public ActionResult DoSomething() {
    // ... perform some server-side operation ...
    return new EmptyResult();
}


Related Question