[英]REST service how to pass JSON for parameters
I have created a WCF REST API: 我创建了WCF REST API:
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json, UriTemplate = "/checkEmail")]
RestResponse<bool> checkEmail(string EmailId);
JSON request is: JSON请求是:
{ "EmailId" :"youremail@yahoo.com" }
It is working as expected. 它按预期工作。 But after some code review changes I changed the param in method to
但是在进行一些代码审查更改之后,我将方法中的参数更改为
checkEmail(string emailId);
ie changed it in to camel case. 即把它改成骆驼的情况。 How can I modify my code to use same JSON request ie API call should remain same
如何修改我的代码以使用相同的JSON请求,即API调用应保持不变
{ "EmailId" :"youremail@yahoo.com" }
This might work for you. 这可能对您有用。 It works in a limited test I did.
它在我做过的有限测试中有效。 I created a class which has two properties:
我创建了一个具有两个属性的类:
[DataContract]
public class EmailParms
{
[DataMember]
[JsonProperty(DefaultValueHandling=DefaultValueHandling.IgnoreAndPopulate)]
public virtual string EmailId { get; set; }
[DataMember]
[JsonProperty(DefaultValueHandling=DefaultValueHandling.IgnoreAndPopulate)]
public virtual string emailId { get; set; }
}
The JsonPropertyAttribute
is part of Json.NET . JsonPropertyAttribute
是Json.NET的一部分。
And then you change your checkEmail
to have: 然后将您的
checkEmail
更改为:
RestResponse<bool> checkEmail(Emailparms emailParms);
The IgnoreAndPopulate
basically deserializes a property that is missing from that json data, and gives it the property's default value. IgnoreAndPopulate
基本上反序列化该json数据中缺少的属性,并为其提供该属性的默认值。
Now, in your method, you just have to check the values of emailId
and EmailId
in emailParms
and decide which to use. 现在,在你的方法,你只需要检查的值
emailId
和EmailId
在emailParms
并决定使用哪一种。
In my test, it worked for any of these: 在我的测试中,它适用于以下任何一种:
{ "EmailId" :"youremail@yahoo.com" }
{ "emailId" :"youremail@yahoo.com" }
{ "EmailId" :"youremail@yahoo.com", "emailId" :"youremail@yahoo.com" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.