Inside controller different action types.

indradaman sharma
indrada...
Participant
54 Points
17 Posts

Hello sir,

I want to know what are the different return types of action have in controller in MVC .net.

It is very helpful for me if you explain it with all there examples.

 

Thank you

Regards,

Indradaman sharma

Views: 8121
Total Answered: 1
Total Marked As Answer: 0
Posted On: 13-May-2015 07:19

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Moderator
1170 Points
210 Posts
     

Hi Indradaman,

The ASP.NET MVC framework maps URLs to classes that are referred to as controllers.
A controller class typically calls a separate view component to generate the HTML markup for the request.

Most action methods return an instance of a class that derives from ActionResult. The ActionResult class is the base for all action results. However, there are different action result types, depending on the task that the action method is performing.

 The class hierarchy is as in the following:
System.Object
System.Web.Mvc.ActionResult

  • System.Web.Mvc.ContentResult
  • System.Web.Mvc.EmptyResult
  • System.Web.Mvc.FileResult
  • System.Web.Mvc.HttpStatusCodeResult
  • System.Web.Mvc.JavaScriptResult
  • System.Web.Mvc.JsonResult
  • System.Web.Mvc.RedirectResult
  • System.Web.Mvc.RedirectToRouteResult
  • System.Web.Mvc.ViewResultBase

ViewResult:
Renders a view as a Web page.

public ViewResult TestAction()
{
return View();
}
// OR
public ActionResult TestAction()
{
return View();
}

 

PartialViewResult:
Renders a partial view, which defines a section of a view that can be rendered inside another view.

public PartialViewResult TestAction()
{
return PartialView();
}
// OR
public ActionResult TestAction()
{
return PartialView();
}

 

RedirectResult:
Redirects to another action method by using its URL.

public RedirectResult TestAction()
{
return Redirect("");
}
// OR
public ActionResult TestAction()
{
return Redirect("");
}

 

RedirectToRouteResult:
Redirects to another action method.

public RedirectToRouteResult TestAction()
{
return RedirectToRoute("");
}
// OR
public ActionResult TestAction()
{
return RedirectToRoute("");
}

 

ContentResult:
Returns a user-defined content type.

public ContentResult TestAction()
{
return Content("");
}
// OR
public ActionResult TestAction()
{
return Content("");
}

 

JsonResult:
Returns a serialized JSON object.

public JsonResult TestAction()
{
return Json("");
}
// OR
public ActionResult TestAction()
{
return Json("");
}

 

JavaScriptResult:
Returns a script that can be executed on the client.

public JavaScriptResult TestAction()
{
return JavaScript("");
}
// OR
public ActionResult TestAction()
{
return JavaScript("");
}

 

FileResult:
Returns binary output to write to the response.

public FileResult TestAction()
{
return File("","");
}
// OR
public ActionResult TestAction()
{
return File("", "");
}

 

EmptyResult:
Represents a return value that is used if the action method must return a null result (void).

[NonAction]
public void TestAction()
{
 
}
// OR
[NonAction]
public void TestAction()
{
 
}

 

Posted On: 13-May-2015 12:26
banner

Blog

Active User (2)

 Log In to Chat