5

I have an ASP.NET web service (.asmx). My service is defined like the following:

[System.Web.Services.WebService(Namespace = "http://tempuri.org/")]
[System.Web.Services.WebServiceBinding(ConformsTo = System.Web.Services.WsiProfiles.BasicProfile1_1)]
public class MyService : System.Web.Services.WebService
{
  [System.Web.Services.WebMethod]
  public string GetResult()
  {
    string result = "";

    int day = System.DateTime.UtcNow.Day;
    if ((day % 1) == 1)
      result = "odd";
    else
      result = "even";
    return result;
  }
}

Currently, if I call this service method, I get the following result:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">even</string>

My issue is, I need to return just the string part. I do NOT want to return the wrapping XML. How do I do this with an .asmx?

Thanks!

user208662
  • 10,869
  • 26
  • 73
  • 86

3 Answers3

4

Does it need to be an .asmx web service for this? I mean, by excluding the SOAP envelope you're essentially saying "this is not a SOAP web service" as it is, so why not take it a step further and make it a regular .aspx page instead of an .asmx web service.

As a page, what you're trying to do would be trivial. Remove all mark-up from the page, use Response.Headers to edit the response headers accordingly, Response.Write() to output your raw text, and Response.End() to close the response.

David
  • 208,112
  • 36
  • 198
  • 279
  • Genius. That's what I was looking for. – user208662 Jan 02 '11 at 21:43
  • @user208662: I now find myself wondering if the same thing can be achieved in an `.asmx` web method. Part of me doubts it, but part of me doesn't. Next time I find myself in front of my development environment I'm going to have to test it out and see what I can come up with. – David Jan 02 '11 at 21:45
  • 1
    @user: You should make an ASHX handler, not an ASPX page. You don't need the overhead of ASPX. – SLaks Jan 04 '11 at 00:14
2

Use json

add the required attribute to your web service and your web method and you get what you want.

Web Service Attribute:[ScriptService]

Web Method Attribute:[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

Read a sample Here

Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
  • Unfortunately, that approach doesn't work :(. I still get the response in XML. Seems odd. I would expect it to be wrapped in Json. Other ideas? – user208662 Jan 02 '11 at 20:57
  • Why? see the result of running example, http://www.williamsportwebdeveloper.com/BookQuery.html – Jahan Zinedine Jan 02 '11 at 21:12
  • 1
    Dave Ward has a great post about this: http://encosia.com/2010/03/03/asmx-and-json-common-mistakes-and-misconceptions/ – chprpipr Jan 02 '11 at 23:37
-1

Why do you want to get rid of the XML part? The code which is generated by the proxy needs a common format so it can understand and read the data that is being returned. Stripping the XML essentially makes your return data unreadable by the client proxy.

Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129