WCF中UriTemplate中的可选参数

时间:2013-06-22 14:19:34

标签: wcf optional-parameters uritemplate

我在this thread中使用了提示并提供了一个默认值,因此当用户没有指定虚拟子目录时,我假设他指的是要列出的所有内容。它有效。

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

但是,指定默认值会更好。但是, default(String) null ,我想发送一个实际值。特别是,我已经把心放在了 String.Empty 上。但是,我注意到以下内容不起作用。服务器端的条件无法识别空字符串( ...其中'type'在(ColName,'','all')中。

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

怎么办?

4 个答案:

答案 0 :(得分:3)

对我来说,它符合“= null”。我的合同如下:

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

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

答案 1 :(得分:0)

你在UriTemplate中确实没有空的默认值,但是你可以拥有一个默认值的东西,在操作中你可以用你想要的实际默认值替换它,如下所示。 / p>

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();
    }
}

答案 2 :(得分:0)

您可以在URITemplate中添加可选参数。但是,它必须是最后一个参数。

Optional UriTemplate parameter using WebGet

答案 3 :(得分:0)

当仅添加'='以获取字符串变量为空的默认值时,它会抛出消息

  

UriTemplate'/ applications / legal / statuses / {serviceId =}'不是   有效; UriTemplate变量声明'serviceId ='提供了一个   路径变量'serviceId'的默认值为空。注意   UriTemplate路径变量不能绑定到null或空值。   有关更多详细信息,请参阅UriTemplate的文档。

改为分配默认值,稍后在代码中查看:

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

服务ID将是默认值。