ASP.NET Web API: Non-descriptive 500 Internal Server Error

前端 未结 10 1235
难免孤独
难免孤独 2020-12-07 16:08

As title says, I’ve got 500 Internal Server Error from GET request to an IQueryable action. The body of the error is empty. That error happens after my action returns result

相关标签:
10条回答
  • 2020-12-07 17:03

    I usually use the Global.asax to catch all error. Here is a code snip you can use

    public void Application_Error(object sender, EventArgs e)
    {
      Exception exc = Server.GetLastError();
      MvcApplication mvcApplication = sender as MvcApplication;
      HttpRequest request = null;
      if (mvcApplication != null) request = mvcApplication.Request;
    }
    
    0 讨论(0)
  • 2020-12-07 17:08

    This may be related to circular reference.

    http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization#handling_circular_object_references

    Try adding the following code to Application_Start method in the Global.asax file:

     var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
     json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All;
    
    0 讨论(0)
  • 2020-12-07 17:08

    I had the same problem, but the source of it was slightly different: I set the CORS policy incorrectly and that gives me 500 Internal server error, but since CORS was not working, the Access-Control-Allow-Origin header was not presented in response and browser can't read the actual response

    I solved it with ChromeDevTools option Copy as cURL which allows me see the response and understand the source of error

    0 讨论(0)
  • 2020-12-07 17:10

    I ran into this same issue. I found Kiran Challa's response helpful in getting the actual exception being thrown outside of my action.

    To solve my issue, setting the ProxyCreationEnabled property of my context to false got me a step further.

    In my scenario, my next exception was due to a circular reference in my models. After cleaning that up, the phantom 500 response was gone. Good luck if you haven't solved this yet!

    0 讨论(0)
提交回复
热议问题