是否有可能“超载”uritemplates?

问题描述:

 [OperationContract] 
    [WebGet(UriTemplate = "/searchresults/{searchTerm}/{searchType}", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)] 
    Message GetSearchResults(string searchTerm, string searchType); 

    [OperationContract] 
    [WebGet(UriTemplate = "/searchresults/{searchTerm}", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)] 
    Message GetSearchResults(string searchTerm); 

这是可能的 - 如果不是,有人可以提出一种替代方案吗?是否有可能“超载”uritemplates?

我发现,这对我来说是最好的解决办法:

[OperationContract(Name = "SearchresultsWithSearchType")] 
    [WebGet(UriTemplate = "/searchresults/{searchTerm}/{searchType=null}", 
    ResponseFormat = WebMessageFormat.Xml)] 
    Message GetSearchResults(string searchTerm, string searchType); 


    [OperationContract(Name = "SearchresultsWithoutSearchType")] 
    [WebGet(UriTemplate = "/searchresults/{searchTerm}", 
    ResponseFormat = WebMessageFormat.Xml)] 
    Message GetSearchResults(string searchTerm); 

此相符:

"http://myservice/searchresults/mysearchterm"

"http://myservice/searchresults/mysearchterm/"

"http://myservice/searchresults/mysearchterm/mysearchtype"

+0

这对你真的有用吗? WCF通常不允许具有相同名称的两个操作。 – 2013-04-21 12:44:08

+0

它确实为我工作 - OperationContract属性的'Name'属性区分了这两者。但是,底层方法仍然需要不同的签名。 – northben 2013-05-24 16:19:09

不,不是真的 - 因为字符串参数searchType可以是NULL--所以您真的没有办法区分这两个URL模板。如果你使用的是非空类型,比如INT或其他类型,那么你将会不同(然后你(和.NET运行时)可以将两个URL模板分开(基于INT是否存在)。

您需要做的只是检查searchType在您的GetSearchResults方法中是否为空或NULL,并相应地采取行动。

[OperationContract] 
[WebGet(UriTemplate = "/searchresults/{searchTerm}/{searchType}", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)] 
Message GetSearchResults(string searchTerm, string searchType); 

,并在您实现:

public Message GetSearchResults(string searchTerm, string searchType) 
{ 
    if(!string.IsNullOrEmpty(searchType)) 
    { 
     // search with searchType 
    } 
    else 
    { 
     // search without searchType 
    } 
    ...... 
} 
+0

谢谢 - 我的问题是: “HTTP://为MyService/SearchResult所/搜索关键词” 或 “HTTP://为MyService/SearchResult所/搜索关键词/”,即没有检索类别部分网址与上述模板不匹配,并返回404.我是否需要默认searchType参数? – Pones 2010-09-09 12:06:06

+1

@pones:ok - 嗯....我的印象是它会匹配那个模板。但似乎你确实需要两个URI模板。感谢您分享您的见解! – 2010-09-09 13:05:04

我通过使用流从客户端传递数据来实现这一点。 您甚至可以使用同一名称但方法名称不同的2个操作。 从前端确保设置的contentType为“文本/ JavaScript的” OR“应用/八位字节流”, 和尝试,如果使用AJAX或从HTML或数据variabke发送数据作为POST jQuery的

对于实施例 [ OperationContract] [WebInvoke(Method =“PUT”,UriTemplate =“user/id/{id} /”,ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped)] string UpdateUser(string id ,System.IO.Stream流);

[OperationContract的] [WebInvoke(方法= “DELETE”,UriTemplate = “用户/ ID/{ID} /”,ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped)] 字符串DeleteUser(字符串ID);

或替代PUT和DELETE的GET和POST