gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:04 25 4
gpt4 key购买 nike

这是我在服务契约(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com