Custom Action Result Sample in MVC: Day 36

So far we have seen the following built-in action result types and the action helper methods that return them.

Action Result Helper Method Link


Content Result: Day 9
RedirectResult Redirect Redirect Result: Day 10
ViewResult View View Result: Day 11
JavaScriptResult JavaScript JavaScript Result: Day 12
JsonResult Json JSON Result: Day 13
HttpNotFound HttpNotFound Result: Day 14
  File File Result: Day 15

Now let's see the custom action result method step-by-step:

  1. Create a MVC project from the "Empty" template.

  2. Right-click on the project and select "Add" > "Class".

    add new class

  3. Name it "XMLResult".

    Class Name

  4. Inherit the XMLResult class from ActionResult and override the "ExecuteResult" method.

    Create an object of "HttpResponseBase" class, that is a base class for classes that provides Http response information from an ASP.NET operations.

    • Set the content type to application/XML.
    • Create a parameterized constructor of the XMLResult class and pass a datatable as the parameter.
    • Assign a datatable objet to the local datatable object inside the constructor.
    • Create a dataset object inside the ExecuteResult method and add a datatable to that object.


  5. Now add a controller. Right-click on "Controllers" then select "Add" >> "Controller".

    Add Controller

  6. Select "MVC 5 Controller – Empty" and press the "Add" button.


  7. Name it as follows:


  8. Create a datatable object and fill it with some values in the Index action result method.

    Return XMLResult with datatable.


  9. Now run the project and you will see that our datatable is converted into XML using our custom action result.


    << Partial View Sample in MVC: Day 35

Similar Articles