Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • ActionResult in Asp.Net MVC

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 266
    Comment on it

    "ActionResult in Asp.Net MVC"

        We all know that ActionResult is a return type mentioned in the Action Method inside a Controller in MVC.

       In this article I will explain it in more detail.

    Getting started:

    ActionResult is an abstract class that has following 11 subtypes:-

    1. ViewResult - Renders a specified view to the response stream
    2. PartialViewResult - Renders a specified partial view to the response stream
    3. EmptyResult - An empty response is returned
    4. RedirectResult - Performs an HTTP redirection to a specified URL
    5. RedirectToRouteResult - Performs an HTTP redirection to a URL that is determined by the routing engine,
                                                                     based on given route data
    6. JsonResult - Serializes a given object to JSON format
    7. JavaScriptResult - Returns a piece of JavaScript code that can be executed on the client
    8. ContentResult - Writes content to the response stream without requiring a view
    9. FileContentResult - Returns a file to the client
    10.FileStreamResult - Returns a file to the client, which is provided by a Stream
    11.FilePathResult - Returns a file to the client

    We can also say that ActionResult is an abstract parent class from which all these subtypes are derived.

    Now go through the following Example:

    public ActionResult Index()
    {
          return View(); 
    }    
    

    In this example it means that we are returning a ViewResult object and this object is automatically type casted to the parent class ActionResult due to Polymorphism.

    What's the Benefit:

    As we all know that MVC is used to create websites or web applications which work on HTTP Protocol. In which we send Http Request and get Http Response, now the Response can be of many types say HTML Response, JSON Response, etc.

    alt text

    So with the help of a general base Class "ActionResult", what ever the response may be you do not have to write different methods for each response you just have one method with dynamic polymorphic response.

    For Example:

    In the code below we are returning ViewResult and JsonResult with the help of general Class ActionResult.

    public ActionResult Dynamic(bool view)
    {
       if (view)
         return View(); // returns simple ViewResult
       else
         return Json(); // returns JsonResult view
    }      
    

    Hope it helps..!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: