我编写了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).
发布评论
评论(2)
您提供的链接说代码正在返回字符串。
如果您希望WCF服务生成HTML响应,则可以尝试以下代码和提供的示例链接。
WCF REST 4是否可以将HTML作为响应格式之一
返回HTML
从我的WCF服务生成HTML响应
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
XML方法是方式,但是使用XML将HTML注入页面:
The Xml method is the way, but using the xml to inject html and script into the page: