Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC WebAPI returning 500 error with no information

I've been having this problem for several days (now fixed and solution noted for anyone that comes across this issue and is pulling their hair out).

After my latest round of code changes to my Silverlight application which uses MVC4 WebAPI for data, I was having a problem with one of my HttpGet Actions which was returning IQueryable<oneofmyclasses>. Using Fiddler2 to watch the request, I could see I was getting an internal server error (500), with no body text to explain why. I received no errors thrown in my Action.

Check 1: I verified that my Action was indeed getting to the return collection.AsQueryable(); line with no errors. It was

Check 2: I verified that my data was serializing to JSON with no 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();

This also worked. I also tested it using XML formatter even though I was pretty sure it only ever used JSON (can't be too careful).

Check 3: Enabled .Net Framework debugging. This time when the error occured (in HttpApplication.cs) VS 2012 caught it.

My error:

Despite having marked the property with these attributes,

[XmlIgnore]
[IgnoreDataMember]
[JsonIgnore]

the .Net Source was calling a get on one of my properties. The catch, it was a write-only property. I simply added

get { return null; }

and the problem was solved.

I probably should have just done Check 3 first, but my previous experience with this error has shown it to usually be an error trying to serialize my objects, which was why I had a bit of a head scratcher when they did serialize properly and the error persisted.

like image 969
Malcolm O'Hare Avatar asked Aug 24 '12 15:08

Malcolm O'Hare


People also ask

How do I fix 500 internal server error IIS?

IIS error The error 500.19 is an internal server error often occurring on a server using Microsoft IIS software. It indicates that the configuration data for the page is invalid. To solve the issue, delete the malformed XML element from the Web. config file or from the ApplicationHost.

How do I fix swagger internal server error?

HTTP 500 (Internal Server Error) is a generalized error message. To get more specific details regarding those failures, please set logging level to "data" for that respective port under "Settings > Dynamic" on API Gateway Manager. Then, run the ReST call again and check the traffic log.


1 Answers

How I solved it:

Enabled .Net Framework debugging. Tools -> Options -> Debugging -> Check 'Enable .NET Framework source stepping'

This time when the error occured (in HttpApplication.cs) VS 2012 caught it.

like image 119
Malcolm O'Hare Avatar answered Oct 11 '22 00:10

Malcolm O'Hare