2012-09-26 31 views
4

我從ControllerApi繼承像這樣(MVC 4)控制器的.NET Web API後返回行動JSON空

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); 
      } 
    } 
} 

我能看到物體用戶信息填充罰款,這將返回200 OK瀏覽器......但內容僅僅是空的JSON {}

這裏是客戶端AJAX中的CoffeeScript

$.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 {} 
+1

類如何'lightUserInfo'定義? – carlosfigueira

+0

ModelState無效? – Cybermaxs

+2

你爲什麼使用HttpResponseMessage ?你還在測試? –

回答

2

不要以爲一個POCO將是最簡單的情況下,絕對連載無線沒有任何麻煩。你必須正確標記。包括

using System.Runtime.Serialization; 

然後標記,像這樣

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

}