Wednesday 21 September 2011

How to create your Custom Help Page In WCF Rest Service

In the Configuration File, make some changes as follows:


<behavior name="AFServiceBehavior">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug httpHelpPageEnabled="false" />
</behavior>

Initially:
httpHelpPageEnabled - false
httpGetEnabled- false
and then Save Configuration File.

Then Go to your IService Class :

[OperationContract]
[WebGet(UriTemplate = "")]
Message GetMainHelpPage();

Implement the following Code in Service.cs Class:

 public Message GetMainHelpPage()
        {
            string page = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html xmlns=""http://www.w3.org/1999/xhtml"">
  <head>
    <title>Operations at http://localhost:35798/Service</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id=""content"">
      <p class=""heading1"">Operations at http://localhost:35798/Service</p>
      <p>Your Service has been created..This page describes the service operations at this endpoint.</p>
      <table>
        <tr>
          <th>Uri</th>
          <th>Method</th>
          <th>Description</th>
        </tr>
        <tr>
          <td>XML</td>
          <td title=""http://localhost:35798/Service/XML?id={id}"">
            <a rel=""operation"" href=""XML/1"">XMLData(string id)</a>
          </td>
          <td>Service at http://localhost:35798/Service/XML</td>
        </tr>
        <tr>
          <td>JSON</td>
          <td title=""http://localhost:35798/Service/JSON?id={id}"">
            <a rel=""operation"" href=""JSON/1"">JSONData(string id)</a>
          </td>
          <td>Service at http://localhost:35798/Service/JSON</td>
        </tr>
     
      </table>
    </div>
  </body>
</html>";
            //return WebOperationContext.Current.CreateJsonResponse(JsonConvert.SerializeObject(page));
            return WebOperationContext.Current.CreateStreamResponse(new MemoryStream(Encoding.UTF8.GetBytes(page)), "text/html");
        }



This will give you HTML custom Help Page.
I will update more in the next article.

1 comment:

  1. If you have any query related to WCF Rest Service Custom Help page, feel free to ask.
    Also, Please give suggestions to improve article.

    ReplyDelete