【问题标题】:RESTful service not returning expected XMLRESTful 服务未返回预期的 XML
【发布时间】:2015-01-15 21:06:00
【问题描述】:

我有一个 WCF Rest Service,它当前返回如下所示的 xml 响应。

但是,它应该以以下格式返回,我无法将其输出。获得正确输出的任何帮助将不胜感激。

<app:get-order-details-response xmlns:apps="http://app.com/123/abc/">
    <organisation-id>123</organisation-id>
    <app2app-id>Mer001</app2app-id>
    <order-id>100</order-id>
    <order-price>0</order-price>
    <response-handler>yourapp://response/parameter1</response-handler>
    <failure-response-handler>yourapp://response/parameter2</failure-response-handler>        
</app:get-order-details-response>

生成当前xml的代码如下所示。是因为我要返回一个 XmlElement 吗?我已经尝试返回 XmlDocument 并更改界面中的代码来执行此操作,但这并不能解决此问题。

public XmlElement GetOrderDetails(string organisationID, string paymentReference, int paymentType, string deviceType)
    {
        .....
        .....
        .....
        XmlDocument doc = new XmlDocument();

        XmlElement root = doc.CreateElement("app:get-order-details-response", "http://app.com/123/abc/");
        doc.AppendChild(root);
        CreateElement(doc, root, "organisation-id", organisationID);
        CreateElement(doc, root, "app2app-id", app2appID);
        CreateElement(doc, root, "order-id", paymentReference;
        CreateElement(doc, root, "order-price", paymentAmount);
        CreateElement(doc, root, "response-handler", responseHandler);
        CreateElement(doc, root, "failure-response-handler", failureResponseHandler);

        return root;
    }

界面:

[ServiceContract]
public interface IPaymentService
{
    [OperationContract]
    [WebInvoke(RequestFormat = WebMessageFormat.Xml, 
        ResponseFormat = WebMessageFormat.Xml, 
        BodyStyle = WebMessageBodyStyle.Wrapped, 
        Method = "GET",
        UriTemplate = "/getorderdetails/v1/aa/order/{organisationID}/{paymentReference}?paymentType={paymentType}&deviceType={deviceType}")]
    XmlElement GetOrderDetails(string organisationID, string paymentReference, int paymentType, string deviceType);
}

更新:

在按照 Pankaj 下面的建议进行修改后,输出将在 IE 中显示。但是,在针对测试工具包进行测试时,我收到一条错误消息,提示“找不到元素 'app:get-order-details-response' 的声明。这是什么意思?

【问题讨论】:

    标签: xml wcf rest response


    【解决方案1】:

    尝试改变

    BodyStyle = WebMessageBodyStyle.Wrapped
    

    BodyStyle = WebMessageBodyStyle.WrappedRequest
    
    OR
    
    BodyStyle = WebMessageBodyStyle.Bare
    

    【讨论】:

    • 谢谢。在 IE 中查看输出时,顶部的修改似乎有效。但是,在针对测试工具包进行测试时,我收到一条错误消息,提示“找不到元素 'app:get-order-details-response' 的声明。这是什么意思?
    • 这意味着它无法找到命名空间“app”的定义。我不知道您使用的是什么工具包或它是如何配置的。可能重新配置您的测试工具应该可以解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    相关资源
    最近更新 更多