Jeff Garoutte

c# .net and anything else that happens across my desk

WebService Documentation: Using a custom template

Web Services: they are an easy way to make information accessible or use information from some other site.  For example, weather.gov's wsdl.  The beauty of web services is that not all web services are .net and we don't have to care.  Any web service that adheres to the w3's standards and that provides a wsdl we can easily use in asp.net.  In order to help people understand how to consume the service; asp.net even gives us a nice page of generated documentation when someone opens the asmx in their browser.

I, for one, hate that page.  If I have gone to all the trouble of building a WebService for someone to submit data or request information, why would I want to send them to a page that looks nothing like the rest of my site to learn how to use it?  At the very least brand it in a way that identifies my company.  But no, the sad state is such that the generate page is fixed and only by adding a theme to the page section in a web.config can you get it to use a custom style sheet or theme.

So what are we to do?  Replace it!  That's right, we can replace it.  The first thing we need is, a web service of course.

I've cracked open the ObjectHelpDesk solution and added a new folder, "help".  I have added a new WebService "help-desk" in the help folder.  I moved the code-behind into a dll and edited the asmx accordingly.

I left HelloWorld in place and added EchoHelloWorld.  Do not forget to but the WebMethod tag on EchoHelloWorld; otherwise the method will not be available in the web service.

imageNow we can browse out to the site and see how it looks.  If you added comments to the code you might be thinking, "It would be a vast improvement if my comments listed under the methods."

I agree, and we can come close; by editing the WebMethod attribute tag, above the methods, we can add a basic description to the default documentation page.  We are going to replace the default page; but we are also going to be using its "guts" as the base of our new page.  Because turning off the custom WebService documentation page is as easy as editing one line in the web.config it is important to make sure everything is in order.   There is also a description property on the WebService tag that can be set as well.

[WebMethod(Description="Echos back the passed in string")]
public string EchoHelloWorld(string echoMessage)
{
    return "Hello World " + echoMessage;
}
(don't forget to set a description for the WebService)
[WebService(Namespace = http://www.JeffGaroutte.net/, 
    Description="This web service is currently used for testing.")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class help_desk : System.Web.Services.WebService

 

Browse out to C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG and look for DefaultWsdlHelpGenerator.aspx.  Copy the file into your project and rename it.  I renamed mine to WsdlTemplate.aspx and placed it in the "help" folder with the asmx.  Tip: Do not delete, remove, edit or rename the original DefaultWsdlHelpGenerator.aspx file.  Sites that do not have a custom template specified in the web.config will need the file.  If for some reason you need to turn off the custom template will will still need the file.

Edit the web.config and add a webServices section. 

image

Notice the href; it looks like a relative path.  If the href starts with ~/ or / asp.net will throw a 404 error.  You can also add a protocols section to the webServices to remove the "documentation" feature entirely.  This is an excellent idea if the WebService is not meant to be open to general public use.  For "security reasons" the HttpPost and HttpGet protocols methods are disabled for any request that is not from the localhost.  

Now you can edit your WsdlTemplate.aspx page and change to your hearts content.  However, the page must be dymancily complied.  I tried moving the huge code block out into a code behind and found that asp.net did not like it.  I believe this is due to the way the rendered page is generated from the template inside of the framework.

Happy Coding!

kick it on DotNetKicks.com

Comments

Home Syria said:

Home

Great posting about this issue. I got new knowledge here. Your article easy to understand. Keep writing and happy blogging.

Regards
bibak

# January 29 2010, 01:50

Home Finland said:

Home

Great posting about this issue. I got new knowledge here. Your article easy to understand. Keep writing and happy blogging.

Regards
bibak

# January 29 2010, 02:36

visage Mongolia said:

visage

excellent.
great article you made. I feel enjoy while reading your article.

thanks

# February 11 2010, 06:15

image masking United States said:

image masking

Great post! I lookin for this, and I'm found in here. Great site. I will visit here again.

# February 26 2010, 09:19

Fatcow Coupon United States said:

Fatcow Coupon

Should I get a Virtual Private Server? Currently I am using anhosting but they keep shutting me down due to high server overload. Im getting about 4,000 UV a day. What brand should I get?

# February 27 2010, 10:06

Fatcow Review United States said:

Fatcow Review

Should I get a Dedicated Hosting? Right now I am using anhosting but they keep disabling my account due to high server load. Im getting about 3,000 unique views a day. What hosting should I get?

# March 01 2010, 12:35

genital warts cures France said:

genital warts cures

It will take about three to four weeks for the garlic to remove your warts.

# March 02 2010, 23:25

wow mobiles United States said:

wow mobiles

WoW Mobile is awesome! I get free mobile service with t-mobile because I refered 3 people to wow. You can too!

# March 03 2010, 05:06

Bobby United States said:

Bobby

Hey there, are you having issues with your hosting? I needed to refresh the page about four times to your site load. You might want to change hosts, or could the problem be from me?

-Bobby

# March 03 2010, 18:07

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
Loading