Search code examples
c#postasp.net-web-apiinternal-server-error

Incomprehensible Error 500 while Posting to Asp.Net Rest Method


I used to have a working Rest method which I did not change at all. I tested it again recently but it now behaves strangely and does not even hit the first line breakpoint. Instead it returns a 500 error: Internal Server Error. I've tested it with Postman with or without parameters and here is the response:

"message": "An error has occurred.", "exceptionMessage": "Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get_Request()'.", "exceptionType": "System.MissingMethodException", "stackTrace": " at AllyNationsWebsite.Controllers.PictureController.d__3.MoveNext()\r\n at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.Start[TStateMachine](TStateMachine& stateMachine)\r\n at AllyNationsWebsite.Controllers.PictureController.PostImage()\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_3.<GetExecutor>b__2(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n ...

My simplified code which reproduces the error is the following:

[HttpPost]
    [Route("ImageCreation")]
    [ResponseType(typeof(long))]
    public async Task<IHttpActionResult> PostImage() //[FromBody]
    {
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        return Ok();}

The odd thing about this is that if I comment the following part of the code, I can reach the method and it returns 200 as it is supposed to :

if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

And my config file:

   <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.6.2" />
    <httpRuntime targetFramework="4.6.2" />
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
    <customErrors mode="Off" />
  </system.web>
<system.webServer>
  <httpErrors errorMode="Detailed" />
  <asp scriptErrorSentToBrowser="true"/>
  <security>
    <requestFiltering>
     <requestLimits maxAllowedContentLength="1073741824" />
    </requestFiltering>
  </security>
<modules runAllManagedModulesForAllRequests="true">
  <remove name="WebDAVModule" />
  <remove name="FormsAuthentication" />
  <remove name="TelemetryCorrelationHttpModule" />
  <add name="TelemetryCorrelationHttpModule" 
 type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, 
  Microsoft.AspNet.TelemetryCorrelation" 
  preCondition="integratedMode,managedHandler" />
  <remove name="ApplicationInsightsWebTracking" />
  <add name="ApplicationInsightsWebTracking" 
  type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, 
  Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
<handlers>
  <remove name="WebDAV" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." 
   verb="GET,HEAD,POST,PUT,DELETE,DEBUG" 
   type="System.Web.Handlers.TransferRequestHandler" 
   preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<validation validateIntegratedModeConfiguration="false" />


Solution

  • I just uninstalled and reinstalled Microsoft.AspNet.WebApi.Core package and it works back again. Maybe it has something to do with the .net version that I changed from 4.5.2 to 4.6.2 a few weeks ago without applying the good modifications to the previously installed packages?!