c# - wcf get 方法 - 如何绑定(bind)到 View 模型?

标签 c# wcf

这是我在服务契约(Contract)中的内容:

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, 
           BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
string HelloWorld(HelloWorldViewModel vm);

其中 HelloWorldViewModel 具有属性 XY

如果我执行 localhost/webservices/HelloWorld?X=1&Y=2,并在 HelloWorld 方法中设置断点,vm 将为空。它不会自动将传入的查询字符串参数绑定(bind)到 View 模型对象中。

我错过了什么吗?谢谢!

最佳答案

您描述的行为具体在 ASP.NET MVC model binding 中实现.

如果您想使用 WCF Web Programming Model 将复杂对象传递给 REST 服务操作,您必须将其以序列化形式包含在 HTTP POST 请求的正文中

在您的情况下,根据放置在 HelloWorld 服务操作上的属性,请求的有效负载应如下所示(请注意省略了 XML 命名空间声明):

<HelloWorld>
    <vm>
        <X>1</X>
        <Y>2</Y>
    </vm>
</HelloWorld>

相关资源:

关于c# - wcf get 方法 - 如何绑定(bind)到 View 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7001191/

相关文章:

c# - .NET Compact Framework、WCF 服务、压缩和 DIGEST 身份验证

c# - Active Directory 在 C# 中找不到所有用户

c# - 覆盖 dll 类属性集

c# - 从单用户桌面应用程序转向多用户开发

c# - ASP.NET中长轮询结合WCF服务监控的例子?

visual-studio - Visual Studio 2013 中的 WSE 2.0 Web 服务客户端

wcf - 数据协定中的 IsReference 属性

c# - 如何拥有 IEnumerable<T>.ToObservableCollection()?

c# - 无法从 DeviceInformation.Id 创建 UsbDevice

WCF REST 服务在不活动后进入休眠状态