C# Web API - Internal Server Error 500
Typically when this happens, it means it is failing to serialize the response. Once your controller returns a USER instance, somewhere WebAPI has to serialize that into the format requested by the client.
In this case the client requested "application/json". The default JsonMediaTypeFormatter uses JSON.Net to turn your C# object into json for the client. Apparently that serialization step is failing, but the response code doesn't tell you exactly why.
The easiest way to see exactly what is happening is to use a custom MessageHandler which forces the body to buffer earlier so you can see the actual exception. Take a look at this blog post for an example to force it to show you the real failure.
Bivo Kasaju
Updated on June 11, 2022Comments
-
Bivo Kasaju almost 2 years
When I have a return type of 'string' in my WebAPI controller, the
SuccessStatusCode
returns 'OK' in my MVC Controller, but when the return type is of a model named 'USER', I get this Internal Server Error. Here's my code:WebAPI:
public class UserController : ApiController { OnlineCenterEntities db = new OnlineCenterEntities(); public USER GetUserInfo(string userName, string domain) { USER userInfo = (from u in db.USERs where u.USER_NAME.ToUpper() == userName.ToUpper() && u.LDAP_NAME.ToUpper() == domain.ToUpper() select u).FirstOrDefault(); return userInfo; } }
MVC Controller that calls the WebAPI:
public class HomeController : Controller { HttpClient client; string url = "http://localhost:61566/api/user/"; public HomeController() { client = new HttpClient(); client.BaseAddress = new Uri(url); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); } public async Task<ActionResult> Index(string userName, string domain) { string GetUserInfoURL = String.Format("GetUserInfo?userName={0}&domain={1}", userName, domain); HttpResponseMessage responseMessage = await client.GetAsync(url+GetUserInfoURL); if (responseMessage.IsSuccessStatusCode) { var responseData = responseMessage.Content.ReadAsStringAsync().Result; var userInfor = JsonConvert.DeserializeObject<USER>(responseData); } return View(); }
USER model:
public partial class USER { public int USER_ID { get; set; } public string USER_NAME { get; set; } public string FIRST_NAME { get; set; } public string LAST_NAME { get; set; } public string LDAP_NAME { get; set; } public string EMAIL { get; set; } }
In my WebAPI, if I change the return type from
USER
tostring
(and of course, change the return variable type to some string (userInfo.FIRST_NAME)), I get theSuccessStatusCode
as 'OK', but as of this code, I getInternal Server Error
withStatusCode: 500
(whatever that means). I have tried inserting breakpoint at every possible points, and I know that the api is returning the result fine. I simply don't understand why the following lineHttpResponseMessage responseMessage = await client.GetAsync(url+GetUserInfoURL);
gives InternalServerError error when I have the return type of USER, and return the whole USER model instead of just one string.
Please don't worry about the
userName
anddomain
parameters that I'm passing to the controllers, they are working fine! -
user772401 almost 8 yearsLet me guess. A circular reference was detected... ;) (EF lazy loading)