通过get方法在wcf服务中传递参数无效
我正在使用wcf服务,希望根据父类别选择返回子类别 这是我的界面:通过get方法在wcf服务中传递参数无效,wcf,Wcf,我正在使用wcf服务,希望根据父类别选择返回子类别 这是我的界面: [ServiceContract] public interface iServiceRegistration { [OperationContract] [WebInvoke(RequestFormat = WebMessageFormat.Json, BodyStyle =WebMessageBodyStyle.WrappedRequest, ResponseFormat = We
[ServiceContract]
public interface iServiceRegistration
{
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, BodyStyle =WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, Method = "GET")]
IEnumerable<SelectListItem> GetSubCategories(int categoryId);
}
public class Service : iServiceRegistration
{
public IEnumerable<SelectListItem> GetSubCategories(int categoryId)
{
//code
}
}
然后0进入我的参数categoryId
有人能告诉我问题出在哪里吗?您将方法指定为
GET
(顺便说一句,不要使用[WebInvoke(method=“GET”)]
;而是使用[WebGet]
)。GET请求中的参数在查询字符串中传递,而不是在请求正文中传递。您应该更改客户端(REST客户端)以发送以下请求:
GET http://your-service/endpoint/GetSubCategories?categoryId=1
或更改方法以接受POST
请求:
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStype = WebMessageBodyStyle.WrappedRequest)]
IEnumerable<SelectListItem> GetSubCategories(int categoryId);
您错过了UriTemplate,在get方法中需要在url中传递参数。 所以你的电话应该是
试试:{“categoryId”:1}不起作用,我认为问题不在这里(意味着从rest客户端传递参数)+1请您解释,但我没有足够的理由投票,非常感谢您的支持
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStype = WebMessageBodyStyle.WrappedRequest)]
IEnumerable<SelectListItem> GetSubCategories(int categoryId);
POST http://your-service/endpoint/GetSubCategories
Content-Type: application/json
Content-Length: XXX
{"categoryId":1}