首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于用户定义参数的WCF REST WebGet

用于用户定义参数的WCF REST WebGet
EN

Stack Overflow用户
提问于 2012-02-18 04:04:15
回答 3查看 16.2K关注 0票数 2

我与WebGet的业务合同定义如下。

代码语言:javascript
复制
[OperationContract]
[WebGet(UriTemplate = "UpdateUserDetails/?configdata={_userConfigData}&configresult={_configResult}&clientip={_clientIP}&adminname={AdminName}")]
public void UpdateUserDetails(UserConfigData _userConfigData, ConfigResult _configResult, string _clientIP, string AdminName)

当我运行这个服务时,我得到了下面的错误。有什么办法解决这个问题吗?

约定“”UserConfigService“”中的操作“”UpdateUserDetails“”具有类型为“”Service1.WCF.UserConfig.UserConfig.UserConfigData“”的名为“”_userConfigData“”的查询变量,但类型“”Service1.WCF.UserConfig.UserConfigData“”不能由“”QueryStringConverter“”转换。“”UriTemplate查询值的变量必须具有可由“QueryStringConverter”转换的类型。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-02-21 17:15:15

我将假设您使用Json对象来请求数据。

应该是这样的:

代码语言:javascript
复制
[OperationContract]
[WebInvoke(UriTemplate = "UpdateUserDetails?_clientIP={_clientIP}&AdminName={AdminName}", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
public void UpdateUserDetails(UserConfigData _userConfigData, ConfigResult _configResult, string _clientIP, string AdminName)  

JSON数据看起来是这样的:

代码语言:javascript
复制
{
    "_userConfigData":{
        "Property1":"value",
        "Property2":"value",
        "Property3":"value"
        ..and so on...
    },
    "_configResult":{
        "Property1":"value",
        "Property2":"value",
        "Property3":"value"
        ..and so on...
    }
}

有一个用于测试Rest服务的很好的应用程序,您可以尝试使用:

Fiddler

附加信息

响应结果“ not not found

您可能没有正确定义终结点或服务地址。你的webconfig文件应该有这样的配置。

代码语言:javascript
复制
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
<bindings>
  <basicHttpBinding>
    <binding name="soapBinding">
      <security mode="None"></security>
    </binding>
  </basicHttpBinding>
  <webHttpBinding>
    <binding name="webBinding"></binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="jsonBehavior">
      <enableWebScript/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="defaultServiceBehavior">
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<services>
  <!-- USING SOAP-->
  <service behaviorConfiguration="defaultServiceBehavior" name="MyProject.WCF.UserConfig.UserConfigService">
    <endpoint address="soap" binding="basicHttpBinding" bindingConfiguration="soapBinding" contract="MyProject.WCF.UserConfig.IUserConfigService"></endpoint>
  </service>
  <!-- USING JSON-->
  <service behaviorConfiguration="defaultServiceBehavior" name="MyProject.WCF.UserConfig.UserConfigService">
    <endpoint address="json" binding="webHttpBinding" bindingConfiguration="webBinding" behaviorConfiguration="jsonBehavior" contract="MyProject.WCF.UserConfig.IUserConfigService"></endpoint>
  </service>
</services>
</system.serviceModel>  

地址看起来像这样:

代码语言:javascript
复制
SOAP
localhost:1706/soap/UserConfigService.svc

JSON
localhost:1706/json/UserConfigService.svc  

为了获得更好的参考,你可以尝试在这里观看:

How to create simple REST Based WCF Service with JSON format

票数 2
EN

Stack Overflow用户

发布于 2012-02-18 04:06:31

你必须使用字符串,你不能使用对象作为查询字符串参数。它不会将你的查询字符串转换成一个对象。这些变量应该定义为字符串。

票数 0
EN

Stack Overflow用户

发布于 2012-02-21 17:38:32

Here's a link on implementing a custom QueryStringConverter,它会做你想做的事情。还注意到(在那篇文章中提到),将(可能)像UserConfigDataConfigResult这样的复杂对象作为POST数据传递可能更好,而不是在URL中传递。考虑到您的方法名为"UpdateUserDetails",本着REST的精神,最好使用POST (WebInvoke)而不是GET (WebGet)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9334643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档