C# 使用 WebGet 的可选 UriTemplate 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15289120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:17:44  来源:igfitidea点击:

Optional UriTemplate parameter using WebGet

c#wcfrest

提问by fiberOptics

I have tried these

我试过这些

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

WCF 服务 URI 模板中的可选参数?由 Kamal Rawat 在博客中发表 | .NET 4.5 on Sep 04, 2012 本节展示了我们如何在 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 apphas no value

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

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

I want to make appoptional. How to achieve this?
Here is the error when apphas no value:

我想成为app可选的。如何实现这一目标?
这是app没有值时的错误:

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

采纳答案by carlosfigueira

You have two options for this scenario. You can either use a wildcard (*) in the {app}parameter, which means "the rest of the URI"; or you can give a default value to the {app}part, which will be used if it's not present.

对于这种情况,您有两种选择。您可以*{app}参数中使用通配符 ( ) ,表示“URI 的其余部分”;或者您可以为{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();
    }
}

回答by Bahaa

A complementary answer regarding default values in UriTemplates using query parameters. The solution proposed by @carlosfigueira works only for path segment variables according to the docs.

关于UriTemplate使用查询参数的 s 中的默认值的补充答案。@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.

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