Asp.Net MVC Action Results

Here we will learn action results in asp.net mvc with examples and different action results available in asp.net mvc with examples.

Asp.Net MVC Action Results

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.

Types of Action Results in Asp.Net MVC

Following are the list of action results available in asp.net mvc

 

  1. ContentResult
  2. EmptyResult
  3. FileResult
  4. JavaScriptResult
  5. JsonResult
  6. PartialViewResult
  7. RedirectResult
  8. RedirectToRouteResult
  9. ViewResult

Contentresult:

 

It returns the user-defined content type.

  

return Content("I its my content result");

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.