WCF REST custom ResponseFormat


  • I'm looking for direction in adding method to my RESTful service where it's response format is neither XML or JSON but, csv or some other custom format.  So that I'd be able to replace the serialization for this function with something that'd return the data with the desired format.

    How would I go about doing this?  Is it even possible?

    For example something like:

            [WebGet(UriTemplate = "getData/{token}/{startTime},{endTime},{pageNum},{maxPageRecords}"), ResponseFormat=WebMessageFormat.Csv ]
            [WebHelp(Comment="Returns the vpobject rows between the given startTime and endTime that are visible to the authenticated user.")]
            public DataRecord GetData(string token, string startTime, string endTime, string pageNum, string maxPageRecords)

    Obviously there isn't a ResponseFormat of csv although I'd like to add one.

    Thanks for any help.
    Wednesday, October 21, 2009 9:00 PM


All replies

  • You can use the raw programming model to do that. Basically, you'd use System.IO.Stream as the output type, and by doing that you're telling WCF that you'll have total control over the output. When the raw programming model is used, the ResponseFormat property of [WebGet] is ignored. You can see an example at
    • Marked as answer by sls1j Thursday, October 22, 2009 1:29 PM
    Thursday, October 22, 2009 5:18 AM
  • Thank you very much that is exactly what I was looking for and 100 times easier than I expected!

    Just set the return type of the method to System.IO.Stream and return a stream with the exact format that I wanted. 
    Thursday, October 22, 2009 1:31 PM