WCF中的URI模板中的附加/可选查询字符串参数 [英] Additional/Optional query string parameters in URI Template in WCF

查看:499
本文介绍了WCF中的URI模板中的附加/可选查询字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WCF中编写了一个简单的REST服务,其中我已经使用相同的URI模板创建了2方法,但是使用不同的方法(POST和GET)。对于GET方法,我还发送以下额外的查询参数:

  [WebInvoke(Method =POST,UriTemplate =users )] 
[OperationContract]
public bool CreateUserAccount(用户用户)
{
//做某事
return restult;
}

[WebGet(UriTemplate =users?userid = {userid}& username = {userName})]
[OperationContract]
public User GetUser (int userid,string userName)
{
//如果用户ID,则
//获取用户UserID
//如果用户名,则
// Get用户由用户名称
//如果没有参数,然后做某事

}


$ b b

当我调用CreateUserAccount与方法POST它工作正常但是当我调用GetUser方法使用GET和只发送一个查询字符串参数(userID或UserName)时,它给出错误HTTP方法不允许,但如果发送两个参数它的wokcher罚款。



任何人都可以帮助我?

解决方案

可选参数并使用WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters来访问所有这些参数。


I have written a simple REST Service in WCF in which I have created 2 method using same URI Template but with different Method(POST and GET). For GET method I am also sending additional query parameters as follows:

    [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

    }

when I call CreateUserAccount with method POST it is working fine but when I call GetUser method using GET and sending only one query string parameter(userID or UserName) it is giving error "HTTP Method not allowed" but if send both parameters its wokrs fine.

Can anyone help me?

解决方案

Do not specify any of the optional parameters and use WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters to access all of them.

这篇关于WCF中的URI模板中的附加/可选查询字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆