How-to: Customize the Generated Web Service Test Page
Did you ever wonder how you could replace the default generated test/help page of a web service by your own page? This could be useful in a number of situations: you could use your company lay-out, provide contact information or documentation, ... First of all: how is the default test page generated? There is a single ASPX file that takes care of this: DefaultWsdlHelpGenerator.aspx, which is located in the X:\<WindowsDir>\Microsoft.NET\Framework\v1.1.4322\CONFIG directory (for the .NET Framework V1.1). It's possible to alter this file, but be aware it will have an effect for all web services running on that machine!
Changing the test page for all the web services on the machine is probably not want we want to do, so a solution that would allow to change the test page for one web service would be nicer. To accomplish that we can use the wsdlHelpGenerator element in the <webServices> tag of the Web.config file. This element has an attribute called href which should contain the file path for the page we want to use as test page. The Web.config could look like this:
<system.web>
<webServices>
<wsdlHelpGenerator href="myDocs.aspx"/>
</webServices>
...
</system.web>
If we now navigate to the web service (.asmx page), the contents of the myDocs.aspx page will be showed. But, we're still not yet there; this test page is used for all the web services of the current project. So if your project contains more then one web service, the myDocs.aspx should contain logic to display the corresponding information for each web service:
ServiceDescriptionCollection serviceDescriptions;
string ServiceName
{
get { return serviceDescriptions[0].Services[0].Name; }
}
private void Page_Load(object sender, System.EventArgs e)
{
//Get the serviceDescriptions
serviceDescriptions = (ServiceDescriptionCollection) Context.Items["wsdlsWithPost"];
if (serviceDescriptions == null)
{
serviceDescriptions = (ServiceDescriptionCollection) Context.Items["wsdls"];
}
switch(ServiceName)
{
case "Service1":
Response.Write("Docs for Service1");
break;
case "Service2":
Response.Write("Docs for Service2");
break;
}
}
First the serviceDescriptions collection is built, so the name of the service for which information is requested can easily retrieved by using the ServiceName function.