2010-09-09 93 views
8
 [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?

回答

9

我發現,這對我來說是最好的解決辦法:

[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

1

不,不是真的 - 因爲字符串參數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

0

我通過使用流從客戶端傳遞數據來實現這一點。 您甚至可以使用同一名稱但方法名稱不同的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