16 February 2017

Different types of Action Results in MVC

In MVC controller may have one or more actions and can return different type of result like if you want to return Json, JavaScript, empty result, return view etc. ActionResults is the base class of all result type. ActionResults is abstract class. As ActionResults is the base class for all type of action results this means return type of ActionResults can be any of the below listed result types. In this topic we will learn how to use ViewResult, PartialViewResult, RedirectResult and JsonResult.

1.ViewResult (View): This return type is used to return a webpage from an action method.

2. PartialviewResult (Partialview): This return type is used to send a part of a 

    view which will be rendered in another view.

3. RedirectResult (Redirect): This return type is used to redirect to any other 


    controller and action method depending on the URL.

4. RedirectToRouteResult (RedirectToAction, RedirectToRoute): This return 


    type is used when we want to redirect to any other action method.

5. ContentResult (Content): This return type is used to return HTTP content 


    type like text/plain as the result of the action.

6. jsonResult (json): This return type is used when we want to return a JSON message.

7. javascriptResult (javascript): This return type is used to return JavaScript 


    code that will run in browser.

8. FileResult (File): This return type is used to send binary output in response.

9. EmptyResult: This return type is used to return nothing (void) in the result.



Advantages of MVC

1. One of the main advantage is that it will be easier to manage the complexity as the application is divided into model,view and controller.

2. It gives us the full control over the behavior of an application as it does not use view state or server-based forms.


3. It provides better support for test-driven development (TDD).


4. You can design the application with a rich routing infrastructure as it uses a Front Controller pattern that processes Web application requests through a single controller.



13 February 2017