Reputation: 464
I would like to accept a post from a 3rd party tool which posts a complex json object. For this question we can assume an object like this:
{
a: "a value",
b: "b value",
c: "c value",
d: {
a: [1,2,3]
}
}
my .net code looks like
asmx:
[WebMethod]
public bool AcceptPush(ABCObject ObjectName) { ... }
class.cs
public class ABCObject
{
public string a;
public string b;
public string c;
ABCSubObject d;
}
public class ABCSubObject
{
public int[] a;
}
This all works perfectly if I pass the object when it is wrapped and named "ObjectName":
{
ObjectName:
{
a: "a value",
b: "b value",
c: "c value",
d: {
a: [1,2,3]
}
}
}
But fails without the object wrapped in an named object. Which is what is posted.
{
a: "a value",
b: "b value",
c: "c value",
d: {
a: [1,2,3]
}
}
I can accept this or any post with a Handler (ashx), but is this possible using a vanilla .Net Webservice (asmx)?
I also tried combinations of:
[WebMethod(EnableSession = false)]
[WebInvoke(
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate="{ObjectName}")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
I suspect UriTemplate or some missing BodyTemplate would work.
Upvotes: 2
Views: 3145
Reputation: 389
You mignt need to remove WebMethod's parameter, and map json string to ABCObject manually.
[WebMethod]
public bool AcceptPush()
{
ABCObject ObjectName = null;
string contentType = HttpContext.Current.Request.ContentType;
if (false == contentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase)) return false;
using (System.IO.Stream stream = HttpContext.Current.Request.InputStream)
using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
{
stream.Seek(0, System.IO.SeekOrigin.Begin);
string bodyText = reader.ReadToEnd(); bodyText = bodyText == "" ? "{}" : bodyText;
var json = Newtonsoft.Json.Linq.JObject.Parse(bodyText);
ObjectName = Newtonsoft.Json.JsonConvert.DeserializeObject<ABCObject>(json.ToString());
}
return true;
}
Hope this helps.
Upvotes: 3