发布到Asp.Net Rest方法时难以理解的错误500

时间:2018-03-30 09:04:49

标签: c# post asp.net-web-api internal-server-error

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

  

“message”:“发生了错误。”,       “exceptionMessage”:“找不到方法:'System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get_Request()'。”,       “exceptionType”:“System.MissingMethodException”,       “stackTrace”:“在AllyNationsWebsite.Controllers.PictureController.d__3.MoveNext()\ r \ n在System.Runtime.CompilerServices.AsyncTaskMethodBuilder 1.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, IDictionary 2个参数,CancellationToken cancellationToken)\ r \ n ---堆栈结束从抛出异常的先前位置追踪--- \ 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();}

关于这一点的奇怪之处在于,如果我评论代码的以下部分,我可以访问该方法并返回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" />

1 个答案:

答案 0 :(得分:0)

我刚刚卸载并重新安装了Microsoft.AspNet.WebApi.Core包,它再次运行。也许这与我几周前从4.5.2改为4.6.2的.net版本有关,而没有对以前安装的软件包进行良好的修改?!