asp.net-mvc

ActionResult

ViewResult

public ActionResult Index()
{
    // Renders a view as a Web page.
    return View();
}

Action methods typically return a result that is known as an action result. The ActionResult class is the base class for all action results. The ActionInvoker decide which type of action result to return based on the task that the action method is performing.

It is possible be explicit about what type to return, but generally it not necessary.

public ViewResult Index()
{
    // Renders a view as a Web page.
    return View();
}

PartialViewResult

public ActionResult PopulateFoods()
{
     IEnumerable<Food> foodList = GetAll();
     
    // Renders a partial view, which defines a section of a view that can be rendered inside another view.
    return PartialView("_foodTable", foodVms);;
}

Action methods typically return a result that is known as an action result. The ActionResult class is the base class for all action results. The ActionInvoker decide which type of action result to return based on the task that the action method is performing.

It is possible be explicit about what type to return, but generally it not necessary.

public PartialViewResult PopulateFoods()
{
    IEnumerable<Food> foodList = GetAll();
    
    // Renders a partial view, which defines a section of a view that can be rendered inside another view.
     return PartialView("_foodTable", foodVms);
}

RedirectResult

public ActionResult Index()
{
    //Redirects to another action method by using its URL.
    return new RedirectResult("https://www.google.com");
}

Action methods typically return a result that is known as an action result. The ActionResult class is the base class for all action results. The ActionInvoker decide which type of action result to return based on the task that the action method is performing.

It is possible be explicit about what type to return, but generally it not necessary.

public RedirectResult Index()
{
    //Redirects to another action method by using its URL.
    return new RedirectResult("https://www.google.com");
}

RedirectToRouteResult

public ActionResult PopulateFoods()
{
    // Redirects to another action method. In this case the index method
    return RedirectToAction("Index");
}

Action methods typically return a result that is known as an action result. The ActionResult class is the base class for all action results. The ActionInvoker decideы which type of action result to return based on the task that the action method is performing.

It is possible be explicit about what type to return, but generally it not necessary.

public RedirectToRouteResult PopulateFoods()
{        
   // Redirects to another action method. In this case the index method
   return RedirectToAction("Index");
}

In case you want to redirect to another action with parameter - you can use RedirectToAction overload:

public ActionResult SomeActionWithParameterFromThisController(string parameterName)
{
   // Some logic
}
.....................
.....................
.....................
return RedirectToAction("SomeActionWithParameterFromThisController", new { parameterName = parameter });

ContentResult

public ActionResult Hello()
{
    // Returns a user-defined content type, in this case a string.
    return Content("hello world!");
}

Action methods typically return a result that is known as an action result. The ActionResult class is the base class for all action results. The ActionInvoker decide which type of action result to return based on the task that the action method is performing.

It is possible be explicit about what type to return, but generally it not necessary.

public ContentResult Hello()
{
    // Returns a user-defined content type, in this case a string.
    return Content("hello world!");
}

You can know more about it here: Asp.Net Mvc: ContentResult vs. string

JsonResult

public ActionResult LoadPage()
{
    Student result = getFirst();
    
    //Returns a serialized JSON object.
    return Json(result, JsonRequestBehavior.AllowGet);
}

Action methods typically return a result that is known as an action result. The ActionResult class is the base class for all action results. The ActionInvoker decide which type of action result to return based on the task that the action method is performing.

It is possible be explicit about what type to return, but generally it not necessary.

public JsonResult LoadPage()
{
    Student result = getFirst();
    
    //Returns a serialized JSON object.
    return Json(result, JsonRequestBehavior.AllowGet);
}

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow