ASP.NET MVC: Create a Custom ActionResult to return XML

Posted by: Malcolm Sheridan , on 4/4/2011, in Category ASP.NET MVC
Views: 110255
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.

mvc xml result

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.

mvc content type

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:

mvc serialize data xml

Or using the same code, I download information about the processes running on the web server like this:

process result

Or using the same code, I could download all of the months in the year like this:

mvc action result

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.

Absolutely Awesome Book on C# and .NET

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!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET, a Telerik Insider and a regular presenter at conferences and user groups throughout Australia and New Zealand. Being an ASP.NET guy, his focus is on web technologies and has been for the past 10 years. He loves working with ASP.NET MVC these days and also loves getting his hands dirty with jQuery and JavaScript. He also writes technical articles on ASP.NET for SitePoint and other various websites. Follow him on twitter @malcolmsheridan


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Bats Ihor on Monday, April 4, 2011 5:40 AM
Very nice article. Thanks!
Comment posted by Bats Ihor on Monday, April 4, 2011 6:32 AM
Very nice article. Thanks!
Comment posted by Kevin B on Tuesday, April 5, 2011 5:30 AM
Great blog you have here! Do you have an ActionResult to generate Sitemaps too?
Comment posted by Malcolm Sheridan on Tuesday, April 5, 2011 6:30 AM
@Kevin B

Thanks.  No I don't but it wouldn't be hard to get that going.  Coming soon!
Comment posted by Kevin B on Wednesday, April 6, 2011 6:44 AM
coming soon? are you going to show how to generate mvc sitemaps in your blog? Can't wait to read this :0
Comment posted by Selim D on Thursday, April 7, 2011 3:09 PM
Thanks for the article!
Modified your XmlResult class to just return an pre-generated XML content.
Now to figure out how to intercept a call that requests a .xml file and return that generated content instead... :-)
Comment posted by Malcolm Sheridan on Thursday, April 7, 2011 6:41 PM
@Selim D

You could build a HttpHandler to intercept those requests.
Comment posted by Selim D on Thursday, April 7, 2011 8:12 PM
Thanks for the article!
Modified your XmlResult class to just return an pre-generated XML content.
Now to figure out how to intercept a call that requests a .xml file and return that generated content instead... :-)
Comment posted by Selim D on Thursday, April 7, 2011 8:15 PM
@Malcolm

Thanks for the suggestion but I just added a route to the global.asax.cs file in the MVC framework and added .xml as a isapi_aspnet supported type so it gets routed through MVC. Works nice actually!
Credit for the technique goes to Phil Haack himself...
Comment posted by Selim D on Thursday, April 14, 2011 3:20 PM
@Malcolm

Thanks for the suggestion but I just added a route to the global.asax.cs file in the MVC framework and added .xml as a isapi_aspnet supported type so it gets routed through MVC. Works nice actually!
Credit for the technique goes to Phil Haack himself...
Comment posted by Selim D on Thursday, April 21, 2011 11:58 AM
@Malcolm

Thanks for the suggestion but I just added a route to the global.asax.cs file in the MVC framework and added .xml as a isapi_aspnet supported type so it gets routed through MVC. Works nice actually!
Credit for the technique goes to Phil Haack himself...
Comment posted by dotnetstep on Wednesday, April 27, 2011 11:04 PM
Hi,
There is some problem with IE8. What i want is when i click on link it will display in browser itself. But i found that when i do such thing i get error. ( I disabled line httpContextBase.Response.AddHeader("content-disposition", "attachment; filename=" + fileName); to make it viewable by browser)
It is working fine in FireFox.
Comment posted by mani on Thursday, April 28, 2011 1:54 AM
Good one
Comment posted by MAhesh Sabnis on Friday, July 8, 2011 5:13 AM
Excellent!!!
Comment posted by Pluc on Friday, June 22, 2012 9:22 AM
Couldn't you use some reflection in there to avoid having to specify a type and enable the use of anonymous types? Either way, good post and a good ground base that I can extend :D Thanks!
Comment posted by Pluc on Friday, June 22, 2012 9:30 AM
You can simply remove the type from the class signature, change the "Data" property type to object and change "typeOf(T)" to "Data.GetType()" and you have removed the need to specify the data type! But I'm still looking if it's possible to use Anonymous type as this changes will throw an exception if used with Anonymous types :(
Comment posted by Pluc on Friday, June 22, 2012 9:43 AM
Combined with the changes I posted along with using a custom XML serializer (based of this post: http://stackoverflow.com/a/2404984/1338607), I acheived to return anonymous types as a XML reply from an action :)
Comment posted by emc on Thursday, October 17, 2013 12:33 AM
can anyone help me here. I have done this and have the exact code but it is not working on my side. should it be deployed first in IIS or its fine running it via vs2012? thanks
Comment posted by emc on Friday, October 18, 2013 1:40 AM
can anyone help me here. I have done this and have the exact code but it is not working on my side. should it be deployed first in IIS or its fine running it via vs2012? thanks
Comment posted by bob on Thursday, May 8, 2014 5:32 AM
serioulsy?! using image for code?

do something smart dude :)
but thanks for the code

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

C# .NET BOOK

C# Book for Building Concepts and Interviews

Tags

JQUERY COOKBOOK

jQuery CookBook