.NET Web API Error Message Returning Empty JSON

I have a controller inherited from ControllerApi like this (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); } } } 

I see that the userinfo object is filled perfectly and this returns 200 OK to the browser ... but the content is just empty json {}

Here is the client side ajax in 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 {} 
+4
source share
1 answer

Do not assume that POCO will be the simplest and be sure to serialize without any problems. You must mark this correctly. Enable

 using System.Runtime.Serialization; 

Then mark the class this way

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

Source: https://habr.com/ru/post/1436476/


All Articles