发布到 Asp.Net Rest 方法时出现无法理解的错误 500

Incomprehensible Error 500 while Posting to Asp.Net Rest Method

我曾经有一个有效的 Rest 方法,我根本没有改变。我最近再次测试了它,但它现在表现得很奇怪,甚至没有达到第一行断点。相反,它 returns 500 错误:内部服务器错误。我已经使用带或不带参数的 Postman 对其进行了测试,这是响应:

"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 ...

我重现错误的简化代码如下:

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

        return Ok();}

奇怪的是,如果我对代码的以下部分进行注释,我可以访问该方法,并且它 returns 200 应该是:

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

我的配置文件:

   <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" />

我刚刚卸载并重新安装了 Microsoft.AspNet.WebApi.Core 包,它又恢复了。可能和我几周前从4.5.2改成4.6.2的.net版本没有对之前安装的包进行很好的修改有关?!