【问题标题】:WCF URITemplateWCF URI模板
【发布时间】:2020-12-06 12:54:30
【问题描述】:

我在现有 WCF 服务中有一个运营合同。现在我正在为 WCF REST api 扩展它,我得到了很多错误,服务中的最后一个是关于参数的。我的代码如下:

    [OperationContract(Name = "Messages", IsOneWay = true)]
    [WebInvoke(Method = "GET",
        UriTemplate = "/Messages/?id={id}&fileId={fileId}",
        ResponseFormat = WebMessageFormat.Xml, 
        BodyStyle = WebMessageBodyStyle.Wrapped)]
    [Description("Inbound Message")]
    void Messages(Guid id, int fileId);

我收到错误:

UriTemplate '/Messages/?id={id}&fileId={fileId}' 无效; 查询字符串的每一部分都必须采用“name=value”的形式, 当 value 不能是复合段时。请参阅文档 UriTemplate 了解更多详情。*

请建议我在 uritemplate 中缺少什么?

【问题讨论】:

  • Selim Yıldız的回复是正确的,“/Messages/?id={id}&fileId={fileId}”是无效的模板字符串,还需要将Guid改为字符串。

标签: rest wcf wcf-rest


【解决方案1】:

您需要从UriTemplate 中删除? 之前的/,并将id 参数更改为字符串。

[OperationContract(Name = "Messages", IsOneWay = true)]
[WebInvoke(Method = "GET",
    UriTemplate = "/Messages?id={id}&fileId={fileId}",
    ResponseFormat = WebMessageFormat.Xml, 
    BodyStyle = WebMessageBodyStyle.Wrapped)]
[Description("Inbound Message")]
void Messages(string id, int fileId);

另请参阅:UriTemplate and UriTemplateTable

【讨论】:

  • 如果我删除 /,我会收到此错误:UriTemplate '/Messages?id={id}&fileId={fileId}' 无效;当 value 不能是复合段时,查询字符串的每个部分都必须采用“name=value”的形式。
  • 我忘了说,你能不能把id参数Guid改成String,再试一次?
  • 有点,但由于我遇到了其他一些问题,所以我正在等待测试所有内容,看看哪个有效。
  • 没关系。如果对您有帮助,请不要忘记将答案标记为已接受,以便对其他人有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-20
  • 1970-01-01
  • 1970-01-01
  • 2011-06-25
相关资源
最近更新 更多