Compartilhar via


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

  • Anonymous
    March 10, 2004
    The comment has been removed

  • Anonymous
    March 10, 2004
    You have been Taken Out! Thanks for the good post.

  • Anonymous
    July 01, 2004
    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.

  • Anonymous
    May 13, 2005
    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.

  • Anonymous
    May 24, 2006
    Awsome!  That's what I needed: textarea and sorted list...

  • Anonymous
    January 14, 2007
    The comment has been removed

  • Anonymous
    February 16, 2007
    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?

  • Anonymous
    October 06, 2007
    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?

  • Anonymous
    January 15, 2008
    The comment has been removed

  • Anonymous
    February 05, 2008
    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.

  • Anonymous
    May 29, 2008
    The comment has been removed

  • Anonymous
    July 22, 2008
    The comment has been removed

  • Anonymous
    November 11, 2008
    How to Check Whether Web Service is Running or Not on Some Button Click Events. http://dotnet-magic.blogspot.com/

  • Anonymous
    May 26, 2009
    PingBack from http://backyardshed.info/story.php?title=stephen-toub-asp-net-web-services-test-page

  • Anonymous
    June 17, 2009
    PingBack from http://pooltoysite.info/story.php?id=2585

  • Anonymous
    June 18, 2009
    PingBack from http://gardendecordesign.info/story.php?id=5854