In this article we will learn about the ActionResult of MVC which is shows the output to the client ,so instead of going deep let us 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 subtype 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 subtype 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.
Syntax
public class EmpController : Controller { public ActionResult Index() { return View(); } }
- PartialViewResult - 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"); } }
- EmptyResult - This action returns the empty response to the client.
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",})); } }
- JsonResult -This action Serializes a given ViewData object into JSON (JavaScript object notation) format.
public class EmpController : Controller { public JsonResult EmpDet() { return Json("HI", JsonRequestBehavior.AllowGet); } }
- JavaScriptResult -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); } }
- ContentResult -It writes content to the response stream without requiring a view.
public class EmpController : Controller { public ContentResult PersonInfo() { return Content("Hi Vithal Wadje"); } }
- FileContentResult -It returns a file to the client.
public class EmpController : Controller { public FileResult DailyReport() { string fileName = @"c:\Vithal Wadje\DailyReport.pdf"; string contentType = "application/pdf"; return new FilePathResult(fileName, contentType); } }
- FileStreamResult - This action returns a file to the client, which is provided by a Stream.
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"); } }
- FilePathResult - 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 ActionResults 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.
Don't Forget To
Don't Forget To
- Subscribe my YouTube Chanel Compile Mode
- Subscribe free email alert of compilemode.com
- Like Facebook page .
- Follow on Twitter.
- Share to your friends
Post a Comment