如何将旧的HREF CGI调用转换为新的WCF服务?

发布于 2025-01-21 09:52:48 字数 1641 浏览 2 评论 0 原文

我编写了WCF服务,该服务旨在替换一些已停产的CGI-bin代码。称为CGI的原始HTML(返回新网站的URL)就像

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

我尝试的新HTML一样,是:

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

将会分发出来的HTML,并将其添加到几个现有网站中。

我已经设置了WCF服务以返回包含所需URL的纯文本字符串,但是当按下相关按钮(在始发站点上)时,浏览器仅打开一个(几乎)一个(几乎)空白页,仅包含一个url作为字符串,而不是打开它是指的页面。

我不确定这只是我的HTML不正确,还是该服务返回错误的类型。这是接口:

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

这是(简化的)方法(i'rowed'来自在这里):

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;
    }

我已经使用JavaScript和A async fetch(),但是有些用户不想将脚本添加到他们的网站上,因此此替代解决方案必须是纯HTML(原始)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巴黎盛开的樱花 2025-01-28 09:52:48

在您的情况下,要返回原始字符串,将ContentType设置为某物
喜欢“文本/普通”,并将数据返回为流。

您提供的链接说代码正在返回字符串。
如果您希望WCF服务生成HTML响应,则可以尝试以下代码和提供的示例链接。
WCF REST 4是否可以将HTML作为响应格式之一
返回HTML
从我的WCF服务生成HTML响应

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

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;
        }
思念绕指尖 2025-01-28 09:52:48

XML方法是方式,但是使用XML将HTML注入页面:

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文