使用可选UriTemplate参数WebGet [英] Optional UriTemplate parameter using WebGet
问题描述
我尝试了这些。
的可选参数在WCF服务URI模板?
发布者卡迈勒·拉瓦特在博客|在9月4日.NET 4.5,2011
本节展示了我们如何在WCF Servuce URI
inShare
传递可选参数
和
但没有为我工作。这里是我的代码:
[WebGet(UriTemplate =RetrieveUserInformation / {哈希} / {应用})]
酒店的公共字符串RetrieveUserInformation(字符串散列,字符串app)
{
}
它的工作原理如果参数被填满:
https://127.0.0.1/Case/Rest/Qr / RetrieveUserInformation / djJUd9879Hf8df /苹果
但不工作,如果应用
没有值
https://127.0.0.1/Case/Rest/Qr/RetrieveUserInformation/djJUd9879Hf8df
我要让应用
可选。如何实现这一结果
以下是错误当应用
没有值:
端点未找到。请参阅该服务帮助页面构造有效的请求到服务。
您对这个场景有两个选项。您可以使用通配符( *
)的 {应用}
参数,这意味着URI的其余部分或者你可以给一个默认值的 {应用}
部分,如果它不存在,它将被使用。
您可以在 http://msdn.microsoft.com/en-us看到有关URI的模板的详细信息/library/bb675245.aspx 和下面的代码显示两种选择。
公共类StackOverflow_15289120
{
[的ServiceContract]
公共类服务
{
[WebGet(UriTemplate =RetrieveUserInformation / {哈希} / {*应用})]
公共字符串RetrieveUserInformation (字符串散列,字符串app)
{
返回哈希+ - +应用;
}
[WebGet(UriTemplate =RetrieveUserInformation2 / {哈希} / {应用=默认})]
公共字符串RetrieveUserInformation2(字符串散列,字符串app)
{
返回哈希+ - +应用;
}
}
公共静态无效测试()
{
串baseAddress =HTTP://+ Environment.MachineName +:8000 /服务;
WebServiceHost主机=新WebServiceHost(typeof运算(服务),新的URI(baseAddress));
host.Open();
Console.WriteLine(主机开);
WebClient的C =新的WebClient();
Console.WriteLine(c.DownloadString(baseAddress +/ RetrieveUserInformation / dsakldasda /苹果));
Console.WriteLine();
C =新的WebClient();
Console.WriteLine(c.DownloadString(baseAddress +/ RetrieveUserInformation / dsakldasda));
Console.WriteLine();
C =新的WebClient();
Console.WriteLine(c.DownloadString(baseAddress +/ RetrieveUserInformation2 / dsakldasda));
Console.WriteLine();
Console.Write(按ENTER关闭主机);
到Console.ReadLine();
host.Close();
}
}
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
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:
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"; or you can give a default value to the {app}
part, which will be used if it's not present.
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.
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();
}
}
这篇关于使用可选UriTemplate参数WebGet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!