2010-09-09 15 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 "overloaded" संभव है?

उत्तर

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

क्या यह वास्तव में आपके लिए काम करता है? डब्ल्यूसीएफ आमतौर पर एक ही नाम के साथ दो संचालन की अनुमति नहीं देता है। –

+0

यह मेरे लिए काम करता था - 'ऑपरेशन कंट्रैक्ट' विशेषता की 'नाम' संपत्ति दोनों को अलग करती है। हालांकि, अंतर्निहित तरीकों को अभी भी विभिन्न हस्ताक्षरों की आवश्यकता है। – northben

1

नहीं, वास्तव में नहीं - क्योंकि स्ट्रिंग पैरामीटर searchType शून्य हो सकता है - इसलिए आपके पास वास्तव में दो यूआरएल टेम्पलेट्स को अलग करने का कोई तरीका नहीं है। यह अलग होगा यदि आप एक गैर-शून्य प्रकार का उपयोग कर रहे थे, जैसे कि INT या कुछ - तो आप (और .NET रनटाइम) दो यूआरएल टेम्पलेट्स को अलग रख सकते हैं (इस तथ्य के आधार पर कि आईएनटी मौजूद है या नहीं)।

आपको यह देखने की ज़रूरत है कि searchType आपकी GetSearchResults विधि में खाली या शून्य है, और तदनुसार कार्य करें।

[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/खोज परिणाम/SEARCHTERM" या "http: // MyService/खोज परिणाम/SEARCHTERM /" यानी खोज-बिना यूआरएल का हिस्सा उपरोक्त टेम्पलेट से मेल नहीं खाएगा और 404 लौटाएगा। क्या मुझे searchType पैरामीटर को डिफॉल्ट करने की ज़रूरत है? – Pones

+1

@pones: ठीक है - हम्म .... मैं इंप्रेशन के तहत था कि यह उस टेम्पलेट से मेल खाएगा। लेकिन ऐसा लगता है कि आपको दो यूआरआई टेम्पलेट्स की ज़रूरत है। अपनी अंतर्दृष्टि साझा करने के लिए धन्यवाद! –

0

मैं धारा का उपयोग कर ग्राहक से डेटा भेजने के द्वारा ये किया। आप एक ही नाम के साथ 2 ऑपरेशन भी कर सकते हैं लेकिन अलग-अलग विधि नाम। सामने के छोर से contentType के रूप में "text/javascript" या "आवेदन/ओकटेट धारा", सेट और अगर AJAX का उपयोग कर या HTML से या डेटा variabke में पोस्ट के रूप में डाटा भेजने की कोशिश करने के लिए सुनिश्चित करें jQuery

उदाहरण [के लिए ऑपरेशन कंट्रैक्ट] [वेबइवोक (विधि = "पुट", UriTemplate = "उपयोगकर्ता/आईडी/{आईडी} /", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] स्ट्रिंग UpdateUser (स्ट्रिंग आईडी , System.IO.Stream स्ट्रीम);

[OperationContract] [WebInvoke (विधि = "हटाएँ", UriTemplate = "उपयोगकर्ता/आईडी/{आईडी} /", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, Bodystyle = WebMessageBodyStyle.Wrapped)] स्ट्रिंग DeleteUser (स्ट्रिंग आईडी);

या स्थानापन्न रख दिया और प्राप्त और पोस्ट के लिए DELETE