nitish dave
nitish dave

Reputation: 25

passing parameter in wcf service through get method not working

i am working on wcf service where i want to return subcategories based on parent category selection.

this is my interface:

[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
        }
     }

i am using REST Client of mozilla to test this service.now when i am calling this method from rest client like this:

{
 "categoryId": "1"
}

then 0 is coming in my parameter categoryId.

can anybody tell me whats the problem?????

Upvotes: 0

Views: 3554

Answers (2)

Pal
Pal

Reputation: 16

You missed UriTemplate and in get method you need to pass parameter in url. so your call should be like

http://yourservice.svc/UriTemplate/parameter

Upvotes: 0

carlosfigueira
carlosfigueira

Reputation: 87218

You specify the method to be GET (as an aside, don't use [WebInvoke(Method = "GET")]; use [WebGet] instead). Parameters in GET requests are passed in the query string, not in the request body. You should either change the client (REST client) to send the following request:

GET http://your-service/endpoint/GetSubCategories?categoryId=1

Or change the method to accept POST requests:

[WebInvoke(Method = "POST",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           BodyStype = WebMessageBodyStyle.WrappedRequest)]
IEnumerable<SelectListItem> GetSubCategories(int categoryId);

And send the request in the body of a POST call:

POST http://your-service/endpoint/GetSubCategories
Content-Type: application/json
Content-Length: XXX

{"categoryId":1}

Upvotes: 2

Related Questions