Brandon
Brandon

Reputation: 702

How does one implement a .Net WebService that does not encapsulate response in XML?

I am writing an series of web interfaces to some data. I have WebMethods to return the data in DataSet and XmlDataDocument format (The XmlDataDocument removes all the schema overhead.)

[WebMethod]
public XmlDataDocument Search_XML( string query ) {
  return new XmlDataDocument( Search_DataSet( query ) );
}
[WebMethod]
public DataSet Search_DataSet( string query ) {
  DataSet result = new DataSet( "SearchResults" );
  //... Populate DataSet here
  return result;
}

I have also created a function that accepts an XSL formatting string and returns the formated results, allowing the client to format an HTML response they can inject right into their webpage:

public string Search_XSL( string query, string xsl ) {
  string result = "";
  XmlDataDocument resultxml = Search_XML( query );
  XslCompiledTransform transform = new XslCompiledTransform();
  using ( StringReader xslstringreader = new StringReader( xsl ) ) {
    using ( XmlReader xslxmlreader = XmlReader.Create( xslstringreader ) ) {
      using ( MemoryStream transformedmemorystream = new MemoryStream() ) {
        using ( StreamWriter transformedstreamwriter = new StreamWriter( transformedmemorystream ) ) {
          try {
            transform.Load( xslxmlreader );
            transform.Transform( resultxml, null, transformedstreamwriter );
            transformedstreamwriter.Flush();
            transformedmemorystream.Position = 0;
            using ( StreamReader transformedreader = new StreamReader( transformedmemorystream ) ) {
              result = transformedreader.ReadToEnd();
            }
          }
          catch ( Exception ex ) {
            result = ex.InnerException.ToString();
          }
        }
      }
    }
  }
  return result;

}

My question is, how do I implement a WebMethod-like interface for this Search_XSL() function so that I can return the resulting string exactly as the function does, without the XML encoding the WebMethod puts around it? Would that be a new Web Form? How do I implement a Web Form with no actual HTML, just accepting form parameters? Not sure where to start here.

Edit: It looks like a "Generic Handler" .ashx file is the way to go. Is this the right approach?

Upvotes: 2

Views: 331

Answers (3)

Oppositional
Oppositional

Reputation: 11211

If you need an HTTP endpoint that processes an HttpContext and returns a custom response, then using IHttpHandler via a Generic Web handler (*.ashx) would be the correct approach to take.

You would read the values from the request query string and then process the request. Your generic handler would use the HttpContext.Response to set the content type of the output stream to text/html and would write the resulting HTML you wish to inject.

Upvotes: 3

rerun
rerun

Reputation: 25505

WCF is the way to go in .net. You can configure your methods to return json or any number of of other type of serializations. While a generic handler could work there is much better support for wcf. Check out this question for more info.

Upvotes: 2

Roland Mai
Roland Mai

Reputation: 31097

WebMethods use SOAP, which means you can't remove the SOAP envelope at the service layer. Your original idea is right, here's what you need to finish:

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/html";
        Response.Write("Hello world");
    }

    protected override void Render(HtmlTextWriter writer)
    {
    }

It's a quick and dirty solution, but it works.

Upvotes: 0

Related Questions