我在WCF中编写了一个简单的REST服务,其中我使用相同的URI模板但使用不同的方法(POST和GET)创建了2个方法.对于GET方法,我还发送其他查询参数,如下所示:
[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
}
Run Code Online (Sandbox Code Playgroud)
当我用方法POST调用CreateUserAccount它工作正常但是当我使用GET调用GetUser方法并且只发送一个查询字符串参数(userID或UserName)时,它给出了错误"HTTP方法不允许",但是如果发送两个参数,那么它就是好的.
谁能帮我?
不要指定任何可选参数,并使用WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters来访问所有这些参数.