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?
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.