【问题标题】:Optional UriTemplate parameter using WebGet使用 WebGet 的可选 UriTemplate 参数
【发布时间】:2013-03-08 07:48:16
【问题描述】:

这些我都试过了

Optional Parameters in WCF Service URI Template? Posted by Kamal Rawat in Blogs | .NET 4.5 on Sep 04, 2012 This section shows how we can pass optional parameters in WCF Servuce URI inShare

Optional query string parameters in URITemplate in WCF

但是对我没有任何作用。这是我的代码:

    [WebGet(UriTemplate = "RetrieveUserInformation/{hash}/{app}")]
    public string RetrieveUserInformation(string hash, string app)
    {

    }

参数填满就可以了:

https://127.0.0.1/Case/Rest/Qr/RetrieveUserInformation/djJUd9879Hf8df/Apple  

但如果app 没有价值则不起作用

https://127.0.0.1/Case/Rest/Qr/RetrieveUserInformation/djJUd9879Hf8df  

我想让app 可选。如何做到这一点?
这是app没有值时的错误:

Endpoint not found. Please see the service help page for constructing valid requests to the service.  

【问题讨论】:

    标签: c# wcf rest


    【解决方案1】:

    对于这种情况,您有两种选择。您可以在 {app} 参数中使用通配符 (*),表示“URI 的其余部分”;或者您可以为{app} 部分指定一个默认值,如果它不存在,将使用它。

    您可以在http://msdn.microsoft.com/en-us/library/bb675245.aspx 上查看有关 URI 模板的更多信息,下面的代码显示了这两种选择。

    public class StackOverflow_15289120
    {
        [ServiceContract]
        public class Service
        {
            [WebGet(UriTemplate = "RetrieveUserInformation/{hash}/{*app}")]
            public string RetrieveUserInformation(string hash, string app)
            {
                return hash + " - " + app;
            }
            [WebGet(UriTemplate = "RetrieveUserInformation2/{hash}/{app=default}")]
            public string RetrieveUserInformation2(string hash, string app)
            {
                return hash + " - " + app;
            }
        }
        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");
    
            WebClient c = new WebClient();
            Console.WriteLine(c.DownloadString(baseAddress + "/RetrieveUserInformation/dsakldasda/Apple"));
            Console.WriteLine();
    
            c = new WebClient();
            Console.WriteLine(c.DownloadString(baseAddress + "/RetrieveUserInformation/dsakldasda"));
            Console.WriteLine();
    
            c = new WebClient();
            Console.WriteLine(c.DownloadString(baseAddress + "/RetrieveUserInformation2/dsakldasda"));
            Console.WriteLine();
    
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();
        }
    }
    

    【讨论】:

    • 事后几年偶然发现,今天帮助了我。谢谢。
    • 我对以下服务方法有类似的情况:[WebInvoke(Method = "POST", UriTemplate = "upload?fileName={fileName}&filePath={filePath}&replace={replace}")] void UploadFile(Stream fileContent, string fileName, string filePath, bool replace);在这里我需要有文件路径和替换是可选的,但上面的解决方案对我不起作用(尝试了两个,即 {*filepath} 或 {filePath=default}。任何解决方案?
    【解决方案2】:

    关于UriTemplates 中使用查询参数的默认值的补充答案。 @carlosfigueira 提出的解决方案仅适用于根据the docs 的路径段变量。

    仅允许路径段变量具有默认值。查询字符串变量、复合段变量和命名通配符变量不允许有默认值。

    【讨论】:

      猜你喜欢
      • 2015-07-31
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多