ASP.NET Web services test page


Did you know it’s possible to customize the test page auto-generated for Web services in ASP.NET?


For example, let’s say you wanted to order the WebMethods such that they’re displayed in alphabetical order.  You can modify the DefaultWsdlHelpGenerator.aspx page which is used to create that test page (%windir%\Microsoft.NET\Framework\v1.1.4322\CONFIG\DefaultWsdlHelpGenerator.aspx).  ASP.NET gathers up the description information for the requested web service/asmx and passes that data to the help generator page.  You can modify this page however you see fit (or you can tell ASP.NET to look in a different location for the generation page by modifying the machine.config’s system.web/webServices/wsdlHelpGenerator node).


If you look in this file, you’ll see around line 1285:


        Hashtable methodsTable = new Hashtable();
        operationExists = false;
        foreach (ServiceDescription description in serviceDescriptions) {
                foreach (PortType portType in description.PortTypes) {
                        foreach (Operation operation in portType.Operations) {
                                string messageName = operation.Messages.Input.Name;
                                if (messageName == null || messageName.Length == 0) messageName = operation.Name;
                                if (messageName == operationName)  operationExists = true;
                                if (messageName == null) messageName = String.Empty;
                                methodsTable[messageName] = operation;
                        }
                }
        }
        MethodList.DataSource = methodsTable;


The code is creating a hashtable of all of the methods available in the Web service and is then binding the repeater you see on the test page that lists all of the methods to this hashtable.  This sheds light one why the methods appear to be in no particular order on the test page: the code is enumerating a Hashtable rather than an ordered list, so the ordering is at the mercy of the hash values generated for messageName and thus the buckets into which the operations are placed.


To get the methods listed in alphabetical order, all we need to do is make a one line change, from:
        Hashtable methodsTable = new Hashtable();
to
        SortedList methodsTable = new SortedList();


Instead of being placed into a Hashtable, the operations will all be placed into the SortedList, indexed by messageName.  When MethodList is bound to methodsTable (which we would probably also want to rename just for clarity’s sake), methodsTable will be enumerated in alphabetical order (thanks to SortedListEnumerator), and thus the methods listed on the test page will be in alphabetical order.


This is, of course, just one simple change you can make to the DefaultWsdlHelpGenerator.aspx page.  The possibilities are limitless ๐Ÿ™‚

Comments (16)

  1. Michael Larionov says:

    No, I did not know! This is pretty cool.

    The thing which drives me nuts is that the text field in the page is not multiline. So I cannot put a big block of multiline text as a web method parameter. So now editing the page I can change the <input type="text"> to <textarea>. I wonder if someone already done that.

    Thanks!

  2. You have been Taken Out! Thanks for the good post.

  3. Volo says:

    Thx for the idea. It’s very useful! ๐Ÿ™‚

    Other suggestion is to change the code to hide methods that you don’t want to publish by using naming conventions.

  4. Brad Greer says:

    Great! Just the solution I needed to automatically send the XML results from the test page to another web page for viewing with a generic XSLT stylesheet.

  5. Mark Dostie says:

    Awsome!  That’s what I needed: textarea and sorted list…

  6. Sam says:

    Hi,

    My test page form URL always comes up wrong and I don’t know why? The form action always has a port number appended and the http does not have an "s" appended (https:) even though the server has SSL.

    Any idea?

  7. What about if we are hosting in some other place that we cannot access this file? Is there another way arround?

    It´s the same for .net 2.0?

  8. I am Brazilian and find solution just here!!

    Its working!

    Thanks!!!!!

  9. Anu Pareek says:

    I am using the Request and Response messages generated by the Web Services Test page to create documentation for our web services. However, I find that any public properties in base classes are not picked up in the sample messages. For example, Class1 extends Class2. Class1 contains property1 and Class2 contains Property2. For a web service method that returns Class1, the sample Response only contains property1 and not property2.

    Can anyone suggest what changes can need to be made to the DefaultWsdlHelpGenerator.aspx to also pick up the public properties from the base class in the sample request/response messages?

    Thanks.

  10. Rospy says:

    that totally saved my ass!!!! thanx

  11. Lorie says:

    In framework 2.0  DefaultWsdlHelpGenerator.aspx Line 1418 it already has the following code:        

    SortedList methodsTable = new SortedList(StringComparer.Ordinal);

    I added webmethod descriptions, but I am still unsure how my web services are being sorted.  It isnโ€™t by function, description or webmethod.  Do you have any suggestions?  Thank you

  12. Ketan says:

    How to Check Whether Web Service is Running or Not on Some Button Click Events.

    http://dotnet-magic.blogspot.com/