作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
这些我都试过了
和
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.
最佳答案
对于这种情况,您有两种选择。您可以在 {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();
}
}
关于c# - 使用 WebGet 的可选 UriTemplate 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15289120/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!