0

我的服务的 UriTemplate 如下所示:

[WebGet(UriTemplate = "LoginUser/authorizationCode/{authorizationCode}/userId/{userId}/intend/{intend}/email/{email}")]

该函数如下所示:

public User LoginUser(string authorizationCode, string userId, string intend, string email)

我平时的电话是这样的:

http://localhost:60522/MyService.svc/json/LoginUser/authorizationCode/111222333/userId/0006435cec5aa5640639b5d08314bb989a43519/intend/yyy/email/abc@web.de

我的下一个任务是使意图和电子邮件参数可选。

所以我的电话必须在所有这些变体中工作:

http://localhost:60522/MyService.svc/json/LoginUser/authorizationCode/111222333/userId/0006435cec5aa5640639b5d08314bb989a43519/intend/yyy/email/abc@web.de

http://localhost:60522/MyService.svc/json/LoginUser/authorizationCode/111222333/userId/0006435cec5aa5640639b5d08314bb989a43519/intend/yyy

http://localhost:60522/MyService.svc/json/LoginUser/authorizationCode/111222333/userId/0006435cec5aa5640639b5d08314bb989a43519/email/abc@web.de

http://localhost:60522/MyService.svc/json/LoginUser/authorizationCode/111222333/userId/0006435cec5aa5640639b5d08314bb989a43519

如何编辑我的模板和函数以使其正常工作?

4

1 回答 1

2

您可以将intent和email的默认值设置为null,如下图:

        [OperationContract]
        [WebGet(UriTemplate = "LoginUser/authorizationCode/{authorizationCode}/userId/{userId}/intend/{intend=null}/{email=null}")]
         string LoginUser(string authorizationCode, string userId, string intend, string email);

我们可以通过以下 URI 访问此服务:

http://localhost:8000/LoginUser/authorizationCode/Test/userId/Test/intend/

http://localhost:8000/LoginUser/authorizationCode/Test/userId/Test/intend/int/

http://localhost:8000/LoginUser/authorizationCode/Test/userId/Test/intend/int/ema/

在这种情况下,如果要设置电子邮件,则必须设置意图。

所以我认为最好的方法是分别为它们编写一个接口,如下所示:

        [OperationContract]
        [WebGet(UriTemplate = "LoginUser/authorizationCode/{authorizationCode}/userId/{userId}/intend/{intend}/email/{email}")]
         string LoginUser1(string authorizationCode, string userId, string intend, string email);
        [OperationContract]
        [WebGet(UriTemplate = "LoginUser/authorizationCode/{authorizationCode}/userId/{userId}/intend/{intend}/")]
        string LoginUser2(string authorizationCode, string userId, string intend);
        [OperationContract]
        [WebGet(UriTemplate = "LoginUser/authorizationCode/{authorizationCode}/userId/{userId}/email/{email}")]
        string LoginUser3(string authorizationCode, string userId, string email);
        [OperationContract]
        [WebGet(UriTemplate = "LoginUser/authorizationCode/{authorizationCode}/userId/{userId}/")]
        string LoginUser4(string authorizationCode, string userId);

不同的URIS会请求不同的接口;

于 2020-07-13T05:23:27.880 回答