开发者

Json.net: How can I return JObject from a [WebMethod]?

I am testing out json.net. I would like to use its linq-to-json syntax to return json from a function attributed [WebMethod] but I am getting errors.

For example if I use in the code behind

[WebMethod, ScriptMethod(UseHttpGet = true)]
public static JO开发者_开发技巧bject GetStuff() {
    return new JProperty("string", "value");
}

Being called by the following javascript:

  PageMethods.GetStuff(/* parameters */, function(data) {
      // do stuff with data
  });

I get the error "Cannot access child value on Newtonsoft.Json.Linq.JValue".

What should I be returning to ensure that my javascript data object gets filled with JSON?


Why not simply returning objects and leaving the JSON serialization to the underlying infrastructure:

public class MyModel
{
    public string Value { get; set; }
}

and in your web method:

[WebMethod, ScriptMethod(UseHttpGet = true)]
public static MyModel GetStuff() {
    return new MyModel {
        Value = "some value"
    };
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜