ActionResult Return Type in MVC
ActionResult Return Type
When we create an Action method in the controller so ActionResult is the return type of
method
public class LearnController: Controller
method
public class LearnController: Controller
{
// GET: Learn
public ActionResult Index()
{
ViewBag.Title = "Learn Controller";
return View();
}
}
above we can see the index method in which return type is ActionResult.
that means all action methods are derived from ActionResult class.
so ActionResult is base class for all action method
that means all action methods are derived from ActionResult class.
so ActionResult is base class for all action method
Types of Action Result
1) ViewResult:-
When we use ViewResult in the method then method responsibility is to render
View to a browser.
When we use ViewResult in the method then method responsibility is to render
View to a browser.
public ViewResult Index() { ViewBag.Title = "Learn Controller"; return View(); }
2) PartialViewResult:-
When we use PartialviewResult instead of ActionResult then the method
responsibility is to render a partial view.
When we use PartialviewResult instead of ActionResult then the method
responsibility is to render a partial view.
public PartialViewResult Index() { ViewBag.Title = "Learn Controller"; return PartialView("partialviewName"); }
3) RedirectResult:-
When we use RedirectResult instead of ActionResult then method responsibility
is to redirect to another action by URL.
When we use RedirectResult instead of ActionResult then method responsibility
is to redirect to another action by URL.
public RedirectResult Index() { ViewBag.Title = "Learn Controller"; return Redirect("www.test.com"); }
4) RedirectToRouteResult:-
When we use RedirectToRouteResult instead of ActionResult then method
responsibility is to redirect to another action by routes .
public ActionResult Index() { return new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { controller = "Learn",
action = "GetName", Id = new int? () })); }
5) ContentResult:- When we use ContentResult instead of ActionResult then method responsibility is
to return plain text.
public ContentResult Index() { return Content("Hello this is Index method!"); }
6) JsonResult:-
When we use JsonResult instead of ActionResult then method responsibility is
to return data in JSON.
public JsonResult Index() { return Json("Hello this is Index method!"); }
7) FileResult:-
When we use FileResult instead of ActionResult then method responsibility is
to return file means if you want to download file then we can use file result.
public FileResult Index() { return new FilePathResult("path to download file"); }
8) EmptyResult:-
When we use EmptyResult instead of ActionResult then method responsibility is
to return the null result.
public EmptyResult Index() { return new EmptyResult(); }
When we use RedirectToRouteResult instead of ActionResult then method
responsibility is to redirect to another action by routes .
public ActionResult Index()
action = "GetName", Id = new int? () }));
to return plain text.
public ContentResult Index()
6) JsonResult:-
When we use JsonResult instead of ActionResult then method responsibility is
to return data in JSON.
public JsonResult Index()
{
return Json("Hello this is Index method!");
}
7) FileResult:-
When we use FileResult instead of ActionResult then method responsibility is
to return file means if you want to download file then we can use file result.
public FileResult Index()
When we use FileResult instead of ActionResult then method responsibility is
to return file means if you want to download file then we can use file result.
public FileResult Index()
{
return new FilePathResult("path to download file");
}
8) EmptyResult:-
When we use EmptyResult instead of ActionResult then method responsibility is
to return the null result.
public EmptyResult Index()
Comments
Post a Comment