WCF Data Services - Custom POST method

て烟熏妆下的殇ゞ 提交于 2019-12-11 14:58:27

问题


I have created a WCF Data Service and it works fine. My Custom methods that are GET type methods work ok as well. The problem is in POST custom method.

The method looks like that:

    [WebInvoke(Method = "POST")]
    public string CustomMethod(string myParameter)
    {
        return "yes" + myParameter;
    }

I also invoke:

    config.SetServiceOperationAccessRule("CustomMethod", ServiceOperationRights.All);

Then in fiddler my request looks like that:

    Method: POST
    URL: http://localhost:1219/DataService.svc/CustomMethod
    Reguest Headers:
    User-Agent: Fiddler
    Host: localhost:1219
    Content-Length: 27
    Content-Type: application/x-www-form-urlencoded
    Request Body:
    myParameter=parameter1value

The method gets called but the "myParameter" parameter is always null. What am I missing?

Thanks for your time.


回答1:


Please refer to Section 10.4.1.3. Invoking an Action for OData 3.

Short story: The content type must be JSON.

If the invoke request contains any non-binding parameter values, the Content-Type of the request MUST be 'application/json', and the parameter values MUST be encoded in a single JSON object in the request body.




回答2:


I believe the way you are passing parameter 'myParameter' may be wrong.

you can try to consume your service using visual studio and then try to pass it.

one more question.. when you call the service as get service, is Content-Type is

Content-Type:application/x-www-form-urlencoded



来源:https://stackoverflow.com/questions/21146152/wcf-data-services-custom-post-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!