2013-06-22 36 views
2

我已经使用了this thread中的提示并提供了一个默认值,以便当用户不指定虚拟子目录时,我假定他意味着所有的东西都是上市。有用。WCF中的UriTemplate中的可选参数

[OperationContract] 
[WebInvoke(UriTemplate = "GetStuff/{type=all}", ...] 
IEnumerable<Stuff> GetStuff(String type); 

但是,改为指定默认值会更好。但是,默认(字符串)null,我想以实际值发送。特别是,我已经设定了我的心脏String.Empty。但是,我注意到以下情况不起作用。服务器端的条件无法识别空字符串(...(ColName,'','all'))中的'type'。

[OperationContract] 
[WebInvoke(UriTemplate = "GetStuff/{type=String.Empty}", ...] 
IEnumerable<Stuff> GetStuff(String type); 

怎么办?

回答

0

您不能在UriTemplate中真正拥有一个空的默认值,但您可以拥有一些意味着默认值的东西,并且在操作中可以用您想要的实际默认值替换它,如下所示。

public class StackOverflow_17251719 
{ 
    const string DefaultValueMarker = "___DEFAULT_VALUE___"; 
    const string ActualDefaultValue = ""; 
    [ServiceContract] 
    public class Service 
    { 
     [WebInvoke(UriTemplate = "GetStuff/{type=" + DefaultValueMarker + "}", ResponseFormat = WebMessageFormat.Json)] 
     public IEnumerable<string> GetStuff(String type) 
     { 
      if (type == DefaultValueMarker) 
      { 
       type = ActualDefaultValue; 
      } 

      yield return type; 
     } 
    } 
    public static void SendBodylessPostRequest(string uri) 
    { 
     HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri); 
     req.Method = "POST"; 
     req.ContentType = "application/json"; 
     req.GetRequestStream().Close(); // no request body 

     HttpWebResponse resp; 
     try 
     { 
      resp = (HttpWebResponse)req.GetResponse(); 
     } 
     catch (WebException e) 
     { 
      resp = (HttpWebResponse)e.Response; 
     } 

     Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription); 
     foreach (string headerName in resp.Headers.AllKeys) 
     { 
      Console.WriteLine("{0}: {1}", headerName, resp.Headers[headerName]); 
     } 

     Console.WriteLine(); 
     Stream respStream = resp.GetResponseStream(); 
     Console.WriteLine(new StreamReader(respStream).ReadToEnd()); 

     Console.WriteLine(); 
     Console.WriteLine(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* "); 
     Console.WriteLine(); 
    } 
    public static void Test() 
    { 
     string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
     WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress)); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     SendBodylessPostRequest(baseAddress + "/GetStuff/nonDefault"); 
     SendBodylessPostRequest(baseAddress + "/GetStuff"); 

     Console.Write("Press ENTER to close the host"); 
     Console.ReadLine(); 
     host.Close(); 
    } 
} 
+0

Darn ...谢谢。 :) –

3

它对我有效“= null”。我的合同看起来像这样:

[WebInvoke(UriTemplate = "UploadFile/{fileName}/{patientId}/{documentId}/{providerName=null}", Method = "POST")] 

void UploadFile(string fileName, string patientId, string documentId, string providerName, Stream fileContents); 
0

添加时只有 '=' 以获得一个默认值是空字符串变量它引发该消息

的UriTemplate“/应用/法律/状态/ {服务Id =} '不是 有效; UriTemplate变量声明'serviceId ='将 空默认值提供给路径变量'serviceId'。请注意, UriTemplate路径变量不能绑定为null或空值。 有关更多详细信息,请参阅UriTemplate的文档。

,而不是指定一个默认值,并在后面的代码检查:

UriTemplate = "/applications/legal/statuses/{serviceId=default}" 

服务ID将缺省值。