Here we will learn action results in asp.net mvc with examples and different action results available in asp.net mvc with examples.
An ActionResult in asp.net mvc is an Abstract class, and ActionResult is the return type of controller method. Each ActionResult returns a different type of result. If we want to display an image in Webforms, we need to create Imagehandler for it, but in Asp.net MVC we can use FileResult that is already a built-in method.
Whenever we create an empty controller in MVC we have the default ActionResult method created with the name Index, and it returns View(). It will have various other options also. Let's have a look at it.
Following are the list of action results available in asp.net mvc
Contentresult:
It returns the user-defined content type.
EmptyResult:
It returns a result that does nothing.
return new EmptyResult();
FileResult:
It returns binary file content to the response.
JavaScriptResult:
It returns a script that will be executed at the client browser.
public JavaScriptResult Examplejavascript()
{
var value = "alert('its JavaScriptResult')";
return JavaScript(value);
}
JsonResult:
It returns a serialized JSON object.
public JsonResult Examplejson()
{
List<string> mystring = newList<string>();
mystring.Add("HI");
mystring.Add("MVC");
return Json(mystring, JsonRequestBehavior.AllowGet);
}
PartialViewResult:
It returns a partial view to the response.
public PartialViewResult ExamplePartialView()
{
return PartialView("Login");
}
RedirectResult:
It will redirect to a specific URI.
return Redirect("/Home/index");
RedirectToRouteResult:
It redirects to another action method.
ViewResult:
It renders a view as a Web page.
return View("Index");
Each action result will perform different actions based on the type of action result in asp.net mvc.