In this article we will learn about the ActionResult of MVC, So instead of going deep lets start with overview .
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.
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.
- ViewResult
- PartialViewResult
- EmptyResult
- RedirectResult
- RedirectToRouteResult
- JsonResult
- JavaScriptResult
- ContentResult
- FileContentResult
- FileStreamResult
- FilePathResult
ViewResult
It returns the a specified view to the response stream such as html.
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.
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.
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.
FileContentResult
public class EmpController : Controller { public ContentResult PersonInfo() { return Content("Hi Vithal Wadje"); } }
It returns a file to the client.
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.
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.
public class EmpController : Controller { public FilePathResult DownloadFile(string file, Guid GuID) { return File(FileStream, "application/octet-stream", fileName); } }
This article is only briefs about the Action Results so beginners can understand,for more details please read my future article.
Summary
My next articles we will learn in depth about each ActionResult. I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.
Post a Comment