4

我有一个像这样从 ControllerApi 继承的控制器(MVC 4)

public HttpResponseMessage<lightUserInfo> Post(LogOnModel model)
{
    if (ModelState.IsValid)
    {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                MembershipUser user = Membership.GetUser(model.UserName);
                var userinfo = new lightUserInfo(user);
                return new HttpResponseMessage<lightUserInfo>(userinfo);
            }
    }
 }

我可以看到对象 userinfo 填充得很好,这会向浏览器返回 200 OK ...但内容只是空 json {}

这是 CoffeeScript 中的客户端 ajax

$.ajax
  url: Meshable.rooturl + "/api/authentication"
  data:   JSON.stringify authenticationDetails    
  dataType: "json"
  type: "POST"
  contentType: 'application/json; charset=utf-8'
  error: (e) ->                     
  success: (data) ->
    #data comes in as empty {}
4

1 回答 1

2

不要假设 POCO 是最简单的情况,并且肯定会毫无问题地进行序列化。您必须正确标记它。包括

using System.Runtime.Serialization;

然后像这样标记课程

[DataContract]
public class UserInfo
{
    [DataMember]
    public string mystr {get; set;}

}
于 2012-09-28T20:17:02.567 回答