在WCF C#中设置响应格式html消息

时间:2013-08-06 09:27:35

标签: c# wcf format message

C#中有一个WCF服务:

[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
class SubscriptionService : ISubscriptionService
{        
    public Message Help(Message m)
    {
        WebOperationContext.Current.OutgoingResponse.ContentType = "text/html; charset=utf-8";
        return new WebHttpHelpPageMessage();
    }
}

[ServiceContract]
interface ISubscriptionService
{        
    [OperationContract]
    [WebInvoke(UriTemplate = "*", Method = "*")]
    Message Help(Message m);
}

来自继承类的消息:

public class WebHttpHelpPageMessage : Message
{
    ...

    protected override void OnWriteBodyContents(System.Xml.XmlDictionaryWriter writer)
    {
        writer.WriteStartElement("html");
        writer.WriteRaw("<head></head><body><p>Hi</p></body>");
        writer.WriteEndElement();
    }
}

浏览器中的结果 - XML,如何将其作为HTML?

1 个答案:

答案 0 :(得分:0)

因为你已经有了xml,我建议你做的是创建一个xml对象。然后,根据您的需要创建xslt(http://msdn.microsoft.com/en-us/library/ms256069.aspx)。

然后,您可以使用xslt将该xml转换为HTML。这样很简单,你可以控制你想要如何显示它。

http://msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform.aspx

如果你采用这种方法,那就足够了。