views:

2899

answers:

3

I'm able to return JSON and partial views (html) as a valid ActionResult, but how would one return an XML string?

+16  A: 

You could use return this.Content(xmlString, "text/xml"); to return a built XML string from an action.

John Downey
This is the reason I love stackoverflow- 100% spot on answers in minutes!
Toran Billups
Great answer, thanks
Rippo
+1  A: 

For JSON/XML I have written an XML/JSON Action Filter that makes it very easy to tackle without handling special cases in your action handler (which is what you seem to be doing).

aleemb
For anyone reading this post - definately check out his filter... it works well. +1 to aleemb for sharing!
Mark
+4  A: 

Use the XmlResult from MvcContrib on Codeplex.

Also this seems to be a duplicate question.

MotoWilliams
not necessarily - the XmlResult class takes an objet *to serialize*, not already-formed XML as a string.
Bobby
I guess I assumed that since part of the questions referenced JSON that object serialization was part of the equation. I suppose a repository or service could be returning a JSON or XML string to the controller to then be returned to the client from the ActionResult, but that would smell odd to me.
MotoWilliams