Asp.Net MVC Action Results

  Total Views : 3528
  Comments : 1
 
  Keywords : What are the Action Results in Asp.Net MVC, Types of Action Results in Asp.Net MVC, Various types of action results in asp.net mvc

Here we will learn action results in asp.net mvc with examples and different type of 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 return type of controller method. Each ActionResult return different type of result. If we want to display image in Webforms we need to create Imagehandler for it but in Asp.net MVC we can use FileResult which is already built in method.

 

Whenever we create a empty controller in MVC we have default ActionResult method created with name Index and it returns View() but we do not have only Return View() option but various other also lets 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 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 return script that will be executed at 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 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 type of action result in asp.net mvc.

 Comments (1)

  • Rajesh Vhadlure tutlane registered profile image
     
    Rajesh Vhadlure   Reply

    very good and systematic explanation

     Aug 30, 2016

Leave a comment

  • Default Tutlane User