简体   繁体   English

使用WebGet的可选UriTemplate参数

[英]Optional UriTemplate parameter using WebGet

I have tried these 我试过这些

Optional Parameters in WCF Service URI Template? WCF服务URI模板中的可选参数? Posted by Kamal Rawat in Blogs | Kamal Rawat在博客中发表| .NET 4.5 on Sep 04, 2012 This section shows how we can pass optional parameters in WCF Servuce URI inShare 2012年9月4日的.NET 4.5本节介绍如何在WCF Servuce URI inShare中传递可选参数

and

Optional query string parameters in URITemplate in WCF WCF中URITemplate中的可选查询字符串参数

But nothing works for me. 但没有什么对我有用。 Here is my code: 这是我的代码:

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

    }

It works if the parameters are filled up: 如果填充参数,它可以工作:

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

But doesn't work if app has no value 但如果app没有价值,则无效

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

I want to make app optional. 我想让app可选。 How to achieve this? 怎么做到这一点?
Here is the error when app has no value: app没有值时,这是错误:

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

You have two options for this scenario. 此方案有两种选择。 You can either use a wildcard ( * ) in the {app} parameter, which means "the rest of the URI"; 您可以在{app}参数中使用通配符( * ),这意味着“URI的其余部分”; or you can give a default value to the {app} part, which will be used if it's not present. 或者您可以为{app}部分提供一个默认值,如果它不存在,将使用该部分。

You can see more information about the URI Templates at http://msdn.microsoft.com/en-us/library/bb675245.aspx , and the code below shows both alternatives. 您可以在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();
    }
}

A complementary answer regarding default values in UriTemplate s using query parameters. 关于使用查询参数的UriTemplate的默认值的补充答案。 The solution proposed by @carlosfigueira works only for path segment variables according to the docs . @carlosfigueira提出的解决方案仅适用于根据文档的路径段变量。

Only path segment variables are allowed to have default values. 只允许路径段变量具有默认值。 Query string variables, compound segment variables, and named wildcard variables are not permitted to have default values. 查询字符串变量,复合段变量和命名通配符变量不允许具有默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM