WCF中UriTemplate中的可选参数 [英] Optional parameter in UriTemplate in WCF
问题描述
我使用了此线程中的提示,并提供了默认值,因此,当用户未指定重要的子目录时,我假设他的意思是列出所有内容.可以.
I've used the hint in this thread and provided a default value, so that when a user doesnät specify the virutal sub-directory, I'm making the assumption that he meant all the stuff to be listed. It works.
[OperationContract]
[WebInvoke(UriTemplate = "GetStuff/{type=all}", ...]
IEnumerable<Stuff> GetStuff(String type);
但是,最好指定一个默认值.但是, default(String)为 null ,我想发送一个实际值.特别是,我对 String.Empty 表示了放心.但是,我注意到以下操作无效.服务器端的条件无法识别空字符串( ...其中(ColName,'','all')中的'type').
However, it would be nicer to specify a default value, instead. However, default(String) is null and I'd like to sent in an actual value. Particularly, I've set my heart on String.Empty. However, I noticed that the following doesn't work. The condition on server side doesn't recognize the empty string (...where 'type' in (ColName, '', 'all')).
[OperationContract]
[WebInvoke(UriTemplate = "GetStuff/{type=String.Empty}", ...]
IEnumerable<Stuff> GetStuff(String type);
该怎么办?
推荐答案
在UriTemplate中,您不能真正拥有默认的空值,但是您可以拥有一些将意味着默认值的东西,并且在操作时您可以替换具有所需的实际默认值,如下所示.
You can't really have an empty default value in an UriTemplate, but you can have something which will mean a default value, and at the operation you can replace that with the actual default value you want, as shown below.
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();
}
}
这篇关于WCF中UriTemplate中的可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!