What is ActionResult ?
ActionResult is the abstract class which is used to show the output to the client in different format as per result view returned by the controller.
The ActionResult are defined into the controller and controller returns to the client (Browser).
The following are the sub type or types of Action Results which are present in MVC which are used as per output requirement of client.
EmptyResult
ContentResult
ActionResult is the abstract class which is used to show the output to the client in different format as per result view returned by the controller.
The ActionResult are defined into the controller and controller returns to the client (Browser).
The following are the sub type or types of Action Results which are present in MVC which are used as per output requirement of client.
- ViewResult
- PartialViewResult
- EmptyResult
- RedirectResult
- RedirectToRouteResult
- JsonResult
- JavaScriptResult
- ContentResult
- FileContentResult
- FileStreamResult
- FilePathResult
Let us learn in brief about each ActionResult are as
ViewResult
ViewResult
It returns the a specified view to the response stream such as html.
Syntax
PartialViewResult
public class EmpController : Controller { public ActionResult Index() { return View(); } }
It returns the partial view to the response stream,mostly the partial
view call from main view.Its same like as user control in Asp.net.
Syntax
public class EmpController : Controller { public ActionResult Index() { return PartialView("_Empdetails"); } }
This action returns the empty response to the client.
Syntax
JsonResult
public class EmpController : Controller { public ActionResult Index() { return new EmptyResult(); } }
- RedirectResult -It redirects to the specified url same as Response.Redirect in Asp.net.
public class EmpController : Controller { public ActionResult Index() { return Redirect("http://www.compilemode.com"); } }
- RedirectToRouteResult - Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data.
public class EmpController : Controller { public ActionResult Index() { filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { action = "View", controller = "EmpController",})); } }
This action Serializes a given View Data object into JSON (JavaScript object notation) format.
Syntax
JavaScriptResult
public class EmpController : Controller { public JsonResult EmpDet() { return Json("HI", JsonRequestBehavior.AllowGet); } }
This action returns a piece of JavaScript code that can be executed on the client side.
Syntax
public class EmpController : Controller { public virtual JavaScriptResult EmpDet() { var script = string.Format(@" MaxNotificaitonsToShow = {0}; ItemsPerPage = 5;", GlobalSettings.MaxNotificaitonsToShow); return JavaScript(script); } }
It writes content to the response stream without requiring a view.
Syntax
FileContentResult
public class EmpController : Controller { public ContentResult PersonInfo() { return Content("Hi Vithal Wadje"); } }
It returns a file to the client.
Syntax
FileStreamResult
public class EmpController : Controller { public FileResult DailyReport() { string fileName = @"c:\Vithal Wadje\DailyReport.pdf"; string contentType = "application/pdf"; return new FilePathResult(fileName, contentType); } }
This action returns a file to the client, which is provided by a Stream.
Syntax
FilePathResult
public class EmpController : Controller { public ActionResult DownloadFile() { string Fileinfo =@"c:\Vithal Wadje\DailyReport.txt"; byte[] data = Encoding.UTF8.GetBytes(Fileinfo ); return File(data, "text/plain","DailyReport.txt"); } }
This action returns a file to the client.
Syntax
public class EmpController : Controller { public FilePathResult DownloadFile(string file, Guid GuID) { return File(FileStream, "application/octet-stream", fileName); } }
0 Comments