是否有可能“超载”? uri 模板?

发布于 2024-09-18 13:22:41 字数 585 浏览 2 评论 0原文

        [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别靠近我心 2024-09-25 13:22:41

我发现这对我来说是最好的解决方案:

    [OperationContract(Name = "SearchresultsWithSearchType")]
    [WebGet(UriTemplate = "/searchresults/{searchTerm}/{searchType=null}", 
    ResponseFormat = WebMessageFormat.Xml)]
    Message GetSearchResults(string searchTerm, string searchType);


    [OperationContract(Name = "SearchresultsWithoutSearchType")]
    [WebGet(UriTemplate = "/searchresults/{searchTerm}", 
    ResponseFormat = WebMessageFormat.Xml)]
    Message GetSearchResults(string searchTerm);

这匹配:

"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:

    [OperationContract(Name = "SearchresultsWithSearchType")]
    [WebGet(UriTemplate = "/searchresults/{searchTerm}/{searchType=null}", 
    ResponseFormat = WebMessageFormat.Xml)]
    Message GetSearchResults(string searchTerm, string searchType);


    [OperationContract(Name = "SearchresultsWithoutSearchType")]
    [WebGet(UriTemplate = "/searchresults/{searchTerm}", 
    ResponseFormat = WebMessageFormat.Xml)]
    Message GetSearchResults(string searchTerm);

this matches:

"http://myservice/searchresults/mysearchterm"

"http://myservice/searchresults/mysearchterm/"

"http://myservice/searchresults/mysearchterm/mysearchtype"

隱形的亼 2024-09-25 13:22:41

不,不是真的 - 因为字符串参数 searchType 可以为 NULL - 所以你实际上没有任何方法来区分这两个 URL 模板。如果您使用不可为 null 的类型(例如 INT 或其他类型),情况会有所不同 - 那么您(和 .NET 运行时)可以将两个 URL 模板分开(基于以下事实:不存在 INT)。

您需要做的只是检查 GetSearchResults 方法中的 searchType 是否为空或 NULL,并采取相应的操作。

[OperationContract]
[WebGet(UriTemplate = "/searchresults/{searchTerm}/{searchType}", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
Message GetSearchResults(string searchTerm, string searchType);

并在您的实施中:

public Message GetSearchResults(string searchTerm, string searchType)
{
   if(!string.IsNullOrEmpty(searchType))
   {
      // search with searchType
   }
   else
   {
      // search without searchType
   }
   ......
}

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 an INT 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 your GetSearchResults method, and act accordingly.

[OperationContract]
[WebGet(UriTemplate = "/searchresults/{searchTerm}/{searchType}", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
Message GetSearchResults(string searchTerm, string searchType);

and in your implementation:

public Message GetSearchResults(string searchTerm, string searchType)
{
   if(!string.IsNullOrEmpty(searchType))
   {
      // search with searchType
   }
   else
   {
      // search without searchType
   }
   ......
}
泪是无色的血 2024-09-25 13:22:41

我通过使用 STREAM 从客户端传递数据来实现这一点。
您甚至可以有两个名称相同但方法名称不同的操作。
从前端确保将 contentType 设置为“text/javascript”或“application/octet-stream”,
并尝试从 HTML 或数据变量中以 POST 形式发送数据(如果使用 AJAX 或 jQuery)

[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "user/id/{id}/", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        string UpdateUser(string id, System.IO.Stream stream);

[OperationContract]
[WebInvoke(Method = "DELETE", UriTemplate = "user/id/{id}/", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        string DeleteUser(string id);

或者用 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

[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "user/id/{id}/", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        string UpdateUser(string id, System.IO.Stream stream);

[OperationContract]
[WebInvoke(Method = "DELETE", UriTemplate = "user/id/{id}/", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        string DeleteUser(string id);

OR substitute PUT and DELETE for GET and POST

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文