views:

184

answers:

3

If the WebMethod returns string it gets serialized to xml.

I want to return byte[] with responding ContentType - can I also specify it?.

Can it be done in ASP.NET web service web method?

+2  A: 

ASMX web services use SOAP and the content type will always be application/soap+xml and the content represent xml. Even if you return a byte[] from your method this array will be base64 encoded into the soap body.

Darin Dimitrov
So there is no way to bypass this limitation? :)
zproxy
It's not a limitation, it's part of the standard. If you don't want to return a response that a SOAP client can understand, then don't use a SOAP (ASMX) page in the first place - use one of the more generic HTTP handlers and write to the Response object.
David
+2  A: 

You can return a byte array from a web service, but it will still be serialised into the response message. (Typically as base-64 in a SOAP XML response.)

If you want to return only the binary content you shouldn't use a web service. Instead you can use Response.BinaryWrite with a regular page with no html content, or context.BinaryWrite in a http handler.

Guffa
+2  A: 

Instead of a web service, use a Generic Handler (.ashx).

A Generic Handler accepts get/post requests and gives you the ability to completely control the output via the HttpContext.

I typically use these for sending files (pdfs, etc) to the browser.

Chris Lively