0

由于 Web 服务请求的响应格式似乎是 Xml 或 Json。但是,由于 HMTL 是一种 XML,我想知道是否可以(和常见的做法)将 HTML 发送回客户端......

4

3 回答 3

1

您可以向客户端发送 xml 响应并在其中嵌入 html 代码。发送 html 不是一种常见的做法,但它是可能的

于 2012-06-21T12:30:34.410 回答
0

您可以直接返回 HTML 并在客户端解释结果,也可以将 HTML 包装在一个结果对象中,然后该对象将是由您的 Web 服务框架编码的 SOAP 或 JSON。

于 2012-06-21T12:28:43.567 回答
0

返回 HTML 不是一般做法,但您可以通过将 HTML 包含在 JSon 或 XML 中的字符串中来返回 HTML

你可以做:

[WebMethod]
public string GetHTMLString()
{
    return "<HTML><TITLE>...";
}

从 wcf 服务返回 html 时发现了这个

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public Stream getHtml()
{
    // get the html
    var html = DoSomethingToGetHtml();  //Not a built-in .Net method ;)

    // we want our result interpreted as plain html
    WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";

    // create a stream from our html because trying to return a string adds an extra header tag
    //      to the response.  Returning a stream returns the html by itself
    var result = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(html));

    // return the result
    return result;
}
于 2012-06-21T12:32:27.933 回答