是否有可能“超载”? uri 模板?
[OperationContract]
[WebGet(UriTemplate = "/searchresults/{searchTerm}/{searchType}", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
Message GetSearchResults(string searchTerm, string searchType);
[OperationContract]
[WebGet(UriTemplate = "/searchresults/{searchTerm}", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
Message GetSearchResults(string searchTerm);
这可能吗?如果不可能,有人可以提出替代方案吗?
[OperationContract]
[WebGet(UriTemplate = "/searchresults/{searchTerm}/{searchType}", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
Message GetSearchResults(string searchTerm, string searchType);
[OperationContract]
[WebGet(UriTemplate = "/searchresults/{searchTerm}", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
Message GetSearchResults(string searchTerm);
Is this possible - if not, can someone suggest an alternative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现这对我来说是最好的解决方案:
这匹配:
"http://myservice/searchresults/mysearchterm"
"http://myservice/searchresults/mysearchterm/"< /code>
“http://myservice/searchresults/mysearchterm/mysearchtype”
I've found that this was the best solution for me:
this matches:
"http://myservice/searchresults/mysearchterm"
"http://myservice/searchresults/mysearchterm/"
"http://myservice/searchresults/mysearchterm/mysearchtype"
不,不是真的 - 因为字符串参数
searchType
可以为 NULL - 所以你实际上没有任何方法来区分这两个 URL 模板。如果您使用不可为 null 的类型(例如INT
或其他类型),情况会有所不同 - 那么您(和 .NET 运行时)可以将两个 URL 模板分开(基于以下事实:不存在 INT)。您需要做的只是检查
GetSearchResults
方法中的searchType
是否为空或 NULL,并采取相应的操作。并在您的实施中:
No, not really - because the string parameter
searchType
can be NULL - so you don't really have any way to distinguish the two URL templates. It would be different if you were using a non-nullable type, like anINT
or something - then you (and the .NET runtime) could keep the two URL templates apart (based on the fact whether or not the INT is present).What you need to do is just check whether
searchType
is empty or NULL in yourGetSearchResults
method, and act accordingly.and in your implementation:
我通过使用 STREAM 从客户端传递数据来实现这一点。
您甚至可以有两个名称相同但方法名称不同的操作。
从前端确保将 contentType 设置为“text/javascript”或“application/octet-stream”,
并尝试从 HTML 或数据变量中以 POST 形式发送数据(如果使用 AJAX 或 jQuery)
,
或者用 PUT 和 DELETE 代替 GET 和 POST
I achieved this by using STREAM to pass data from client.
You can even have 2 operations with same name but different method name.
from front end make sure to set contentType as "text/javascript" OR "application/octet-stream",
and try sending data as POST from HTML or in data variabke if using AJAX or jQuery
For Example
OR substitute PUT and DELETE for GET and POST