Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASMX Returning a pure string

Tags:

c#

asmx

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!

like image 718
user208662 Avatar asked Jan 02 '11 20:01

user208662


2 Answers

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.

like image 125
David Avatar answered Sep 28 '22 04:09

David


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

like image 20
Jahan Zinedine Avatar answered Sep 28 '22 02:09

Jahan Zinedine