WCF:UriTemplate 中的多个可选参数

WCF: Multiple optional parameters in UriTemplate

我的服务的 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

我的下一个任务是使 intend 和 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

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

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

可以将intend和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/

在这种情况下,如果要设置email,则必须设置intend。

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

        [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会请求不同的接口;