我撰寫了一個 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);
}
這是(簡化的)方法(我從這里“借用”了代碼):
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 和 an 完成了所有作業 async fetch(),但是有些用戶不想將腳本添加到他們的網站,所以這個替代解決方案必須是純 HTML(就像原來的一樣)。
uj5u.com熱心網友回復:
在您的情況下,要回傳原始字串,請將 ContentType 設定為“text/plain”之類的內容并將您的資料作為流回傳。
您提供的鏈接說代碼正在回傳一個字串。
如果您希望 WCF 服務生成 html 回應,您可以嘗試下面的代碼和提供的示例鏈接。
在 WCF REST 4 中是否可以將 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;
}
uj5u.com熱心網友回復:
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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/464460.html
上一篇:Djangorest框架:單元測驗發布請求獲取狀態碼400
下一篇:將C#6與WCF結合使用
