【问题标题】:How do I access the request body in XML format in a WCF REST service?如何在 WCF REST 服务中访问 XML 格式的请求正文?
【发布时间】:2012-05-23 17:12:08
【问题描述】:

我可以创建 WCF REST 服务和 POST、PUT 和 GET 数据 OK。

如何在服务端访问XML格式的请求体发送到SQL数据库?

【问题讨论】:

    标签: wcf web-services rest


    【解决方案1】:

    您实际上可以使用此属性将参数传递给您的网络方法

    [WebGet(UriTemplate = "users/{username}")]
    

    这是来自msdn的示例方法

    [WebGet(UriTemplate = "users/{username}")]
    [OperationContract]
    User GetUserAccount(string username)
    {
        if (!IsUserAuthorized(username))
        {
            WebOperationContext.Current.OutgoingResponse.StatusCode =
                HttpStatusCode.Unauthorized;
            return;
        }
        User user = FindUser(username);
        if (user == null)
        {
            WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound();
            return null;
        }
        return user;
    }
    

    【讨论】:

    • 感谢您的回复。忘记添加请求正文是一个 xml 文档。所以一旦发布到服务,我需要将该文档保存到 sql
    • RequestFormat=WebMessageFormat.Xml webinvoke 的属性应该可以帮助你。例如:[WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Wrapped,RequestFormat=WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json)]
    • 我仍然无法将身体放入数据集。你有什么例子吗?
    【解决方案2】:

    在 MVC3 中,Request 对象在控制器中可用,而正文的内容在 InputStream 对象中可用。这段代码对我有用:

            this.Request.InputStream.Position = 0;
            var xmlContent = new System.IO.StreamReader(this.Request.InputStream).ReadToEnd();
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 2011-10-11
      • 2016-11-16
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      相关资源
      最近更新 更多