2011-02-11 23 views
4

我在WCF中编写了一个简单的REST服务,其中我使用相同的URI模板创建了2个方法,但使用了不同的方法(POST和GET)。对于GET方法,我也派遣更多的查询参数如下:WCF中的URI模板中的附加/可选查询字符串参数

[WebInvoke(Method = "POST", UriTemplate = "users")] 
    [OperationContract] 
    public bool CreateUserAccount(User user) 
    { 
     //do something 
     return restult; 
    } 

    [WebGet(UriTemplate = "users?userid={userid}&username={userName}")] 
    [OperationContract] 
    public User GetUser(int userid, string userName) 
    { 
     // if User ID then 
     // Get User By UserID 
     //else if User Name then 
     // Get User By User Name 
     //if no paramter then do something 

    } 

当我打电话CreateUserAccount与POST方法它工作正常,但是当我打电话的getUser方法使用GET和只发送一个查询字符串参数(用户ID或用户名)它给出错误“不允许使用HTTP方法”,但是如果发送两个参数的话,它的工作正常。

任何人都可以帮助我吗?

+0

你有两个相同的基础URI,你有没有尝试从'用户'chaning一个到别的东西?只询问是因为重载在Web服务中不起作用。 – iMortalitySX 2012-10-29 17:43:35

回答

5

不要指定任何可选参数,并使用WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters来访问所有参数。

相关问题