MVC WebAPI returns 500 errors without information

I had this problem for several days (now fixed and a solution is noted for everyone who encounters this problem and pulls out their hair).

After my last round of code changes in my Silverlight application that uses MVC4 WebAPI for data, I had a problem with one of my HttpGet actions that returned IQueryable<oneofmyclasses> . Using Fiddler2 to view the request, I could see that I was getting an internal server error (500), without the main text, to explain why. I did not get errors in my Action.

Check 1: I confirmed that my action really falls into the string return collection.AsQueryable(); no mistakes. It was

Check 2:. I confirmed that my data was serialized in JSON without errors using this code (g is my collection):

 var json = new JsonMediaTypeFormatter(); json.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects; ObjectContent<IEnumerable<Model.MenuGroup>> responseContent = new ObjectContent<IEnumerable<Model.MenuGroup>>(g, json); MemoryStream ms = new MemoryStream(); responseContent.CopyToAsync(ms).Wait(); ms.Position = 0; var sr = new StreamReader(ms); var str = sr.ReadToEnd(); 

That also worked. I also tested it using XML formatting, although I was sure that it had ever used JSON (cannot be too careful).

Check 3: Enabled. Debugging the .NET Framework. This time, when an error occurred (in HttpApplication.cs), VS 2012 caught it.

My mistake:

Although it marks the property with these attributes,

 [XmlIgnore] [IgnoreDataMember] [JsonIgnore] 

.Net Source caused one of my properties to be received. Catch, this was a write-only property. I just added

 get { return null; } 

and the problem was resolved.

I probably needed to do Check 3 first, but my previous experience with this error showed that this is usually an error trying to serialize my objects, and that is why I had a little head scraper when they were serialized correctly and the error persists.

+6
source share
1 answer

How I decided it:

It is included. Debugging the .NET Framework. Tools -> Options -> Debugging -> Check "Enable .NET Framework Original Step"

This time, when an error occurred (in HttpApplication.cs), VS 2012 caught it.

+6
source

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


All Articles