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
Optional UriTemplate parameter using WebGet
提问by fiberOptics
I have tried these
我试过这些
and
和
Optional query string parameters in URITemplate in WCF
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. How to achieve this?
Here is the error when app
has 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 UriTemplate
s 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.
仅允许路径段变量具有默认值。查询字符串变量、复合段变量和命名通配符变量不允许具有默认值。