这是我在服务契约(Contract)中的内容:
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
string HelloWorld(HelloWorldViewModel vm);
其中 HelloWorldViewModel
具有属性 X
和 Y
。
如果我执行 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/