Home > OS >  How to convert an old href cgi call to a new wcf service?
How to convert an old href cgi call to a new wcf service?

Time:04-17

I have written a WCF service which is designed to replace some defunct cgi-bin code. The original HTML which calls the cgi (which returns the Url of a new site to visit) was like this

href="http://example.com/cgi-bin/webring/list.cgi?ringid=xxx;siteid=47"

The new HTML that I am trying is this:

href="http://example.com/webring.svc/RJump/47,xxx"

The resulting HTML will be distributed, to be added to several existing websites.

I have set up the WCF service to return a plain text string containing the required Url, but when the relevant button is pressed (on the originating site), the browser just opens an (almost) blank page containing just the Url as a string, rather than opening the page it refers to.

I'm not sure if it's just my HTML being incorrect, or if the service is returning the wrong type. Here is the interface:

[ServiceContract]
public interface IWebRing
{
    // bare response for calling from HTML
    [OperationContract]
    [WebGet(UriTemplate = "/RJump/{data}",  BodyStyle = WebMessageBodyStyle.Bare)]
    System.IO.Stream RJump(string data);
 }

and this is the (simplified) method (I 'borrowed' the code from here):

public System.IO.Stream RJump(string data)
    {
        string Url = "http://example.net";
    // 'data' will define which site is actually required
        System.ServiceModel.Web.OutgoingWebResponseContext context = System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse;
        context.Headers.Add(System.Net.HttpResponseHeader.CacheControl, "public");
        context.ContentType = "text/plain";
        context.LastModified = DateTime.Now;
        context.StatusCode = System.Net.HttpStatusCode.OK;
        System.IO.Stream result = new System.IO.MemoryStream(System.Text.ASCIIEncoding.Default.GetBytes(Url));
        return result;
    }

I had already got everything working using javascript and an async fetch() , but some users don't want to add script to their sites so this alternative solution must be pure HTML (as was the original).

CodePudding user response:

In your case, to return a raw string, set the ContentType to something like "text/plain" and return your data as a stream.

The link you provided says the code is returning a string.
If you want the WCF service to generate an html response, you can try the code below and the sample link provided.
Is it possible in WCF REST 4 to return HTML as one of the response formats
Generating html Response from my WCF service

 public XmlElement EchoWithGet(string s)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
            var x = new XmlDocument();
            x.LoadXml("<x>123</x>");
            return x.DocumentElement;
        }

CodePudding user response:

The Xml method is the way, but using the xml to inject html and script into the page:

public XmlElement RJump(string data)
{
    String Url = functionOf(data);
    
    WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
    var x = new XmlDocument();
    x.LoadXml("<html><head><script>window.open('"   Url   "', '_self'); </script></head></html>");
    return x.DocumentElement;
}
  • Related