简体   繁体   中英

Accepting a json request with a Webservice (asmx)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM