ASP.NET MVC: Create a Custom ActionResult to return XML
Posted by: Malcolm Sheridan
in Category ASP.NET MVC
Abstract: The following article outlines the process of creating a new custom ActionResult to return XML data in ASP.NET MVC.
If you’re familiar with ASP.NET MVC, you know how important the ActionResult class is. This class encapsulates the result of an action method and is used to perform a framework-level operation on behalf of the action method. If you’re expecting JSON from an action method, you can either set the return type as JsonResult or leave it as ActionResult. Apart from void, all action methods return type derives from ActionResult. One content type that isn’t in the framework is XML. Sometimes you need to return XML, so I thought I’d demonstrate how you might do this. Once again this is only one way of doing this.
Open studio 2010 and create a new ASP.NET MVC 3 Web Application (Razor) project. Let’s first start be creating the custom ActionResult. All that needs to be done is create a class that derives from ActionResult. I’m going to create a generic class that can handle multiple types of objects and serialize them to XML using the same piece of code. I’ve called the class XmlResult and here it is.
The only code that you override is ExecuteResult. This method is invoked after the action has executed. To force the browser to display the Save As dialog box I’m setting the content disposition. Then I need to set the content type as text/xml. This lets the browser know what type of information is being sent to the user.
The next thing to do is serialize the data to XML. Now to begin using this! Because the data the object is expecting is generic, I could create an action method to return a list of numbers like this:
Or using the same code, I download information about the processes running on the web server like this:
Or using the same code, I could download all of the months in the year like this:
Now you’ve got one ActionResult that will perform many functions. The beauty of this is you’re not repeating code.
I am aware that there are other ways to serialise objects to XML and this may not be the best way to do this, but I wanted to focus on creating a custom ActionResult because I feel an XmlResult should be in the MVC framework. This may be a deficiency in the framework, but this is one way to get around this problem.
The entire source code of this article can be downloaded over here
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!