更新到5.1.0后将数据发布到WebApi失败

我有一个使用webapi加载/保存数据的Web应用程序所有用于工作正常/今天我发现有一个更新到ASP.NET MVC / webapi,我安装了更新但是在那之后,jquery的POSTing数据不再有效。 我在客户端代码或webapi控制器中没有做任何更改。

我的典型POSt语句如下所示:

showProgress(true, self.PanelID(), 'Saving...') j$.ajax({ type: "POST", url: uri, data: dataObj, error: function (jqXHR, status, error) { showProgress(false); GLOBAL.setMessage(self, 'Error saving ' + opType + '
' + error, true); }, success: function (data, status, jqXHR) { showProgress(false); if (status === 'success') { GLOBAL.setMessage(self, opType + " saved ok"); //....

$ .ajax代码收到以下错误

 “发生了错误。”

 ExceptionMessage
     “在调用HttpRequest.Form,Files,InputStream或BinaryRead后,不支持此方法或属性。”

 ExceptionType
     “System.Web.HttpException”

堆栈跟踪
在System.Web.HttpRequest.GetInputStream(Boolean persistEntityBody,Boolean disableMaxRequestLength) 
在System.Web.HttpRequestWrapper.GetBufferedInputStream() 
在System.Web.Http.WebHost.SeekableBufferedRequestStream..ctor(HttpRequestBase请求) 
在System.Web.Http.WebHost.HttpControllerHandler.c__DisplayClass11.b__b()处的System.Web.Http.WebHost.HttpControllerHandler.LazyStreamContent.get_StreamContent()处于System.Web.Http.WebHost.HttpControllerHandler.LazyStreamContent.CreateContentReadStreamAsync()处于System System.Net.Http.HttpContentExtensions.d__0`1.MoveNext()中的.Net.Http.HttpContent.ReadAsStreamAsync() 
 ---从抛出exception的先前位置开始的堆栈跟踪结束--- 
在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 
在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 
在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
在System.Web.Http.ModelBinding.FormatterParameterBinding.d__0.MoveNext() 
 ---从抛出exception的先前位置开始的堆栈跟踪结束--- 
在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 
在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 
在System.Runtime.CompilerServices.TaskAwaiter.GetResult() 
在System.Web.Http.Controllers.HttpActionBinding.d__0.MoveNext() 
 ---从抛出exception的先前位置开始的堆栈跟踪结束--- 
在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
 System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()的System.Runtime.CompilerServices.TaskAwaiter.GetResult()处的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)处于抛出exception的上一个位置---在System.Runtime.CompilerServices.Throw上的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务),System.Runtime.CompilerServices.TaskAwaiter`1.GetResult上的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) ) 
在System.Web.Http.Filters.AuthorizationFilterAttribute.d__2.MoveNext() 
 ---从抛出exception的先前位置开始的堆栈跟踪结束--- 
在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 
在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 
在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
在System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()“

这些是我的解决方案中的nuget包

   package id =“Microsoft.AspNet.Mvc”version =“5.1.0”targetFramework =“net45” 
   package id =“Microsoft.AspNet.Razor”version =“3.1.0”targetFramework =“net45” 
   package id =“Microsoft.AspNet.SignalR.Client”version =“2.0.1”targetFramework =“net45” 
   package id =“Microsoft.AspNet.Web.Optimization”version =“1.1.2”targetFramework =“net45” 
   package id =“Microsoft.AspNet.WebApi”version =“5.1.0”targetFramework =“net45” 
   package id =“Microsoft.AspNet.WebApi.Client”version =“5.1.0”targetFramework =“net45” 
   package id =“Microsoft.AspNet.WebApi.Core”version =“5.1.0”targetFramework =“net45” 
   package id =“Microsoft.AspNet.WebApi.HelpPage.VB”version =“5.1.0”targetFramework =“net45” 
   package id =“Microsoft.AspNet.WebApi.OData”version =“5.1.0”targetFramework =“net45” 
   package id =“Microsoft.AspNet.WebApi.WebHost”version =“5.1.0”targetFramework =“net45” 
   package id =“Microsoft.AspNet.WebPages”version =“3.1.0”targetFramework =“net45” 
   package id =“Microsoft.Bcl”version =“1.1.6”targetFramework =“net45” 
   package id =“Microsoft.Bcl.Build”version =“1.0.13”targetFramework =“net45” 
   package id =“Microsoft.Data.Edm”version =“5.6.0”targetFramework =“net45” 
   package id =“Microsoft.Data.OData”version =“5.6.0”targetFramework =“net45” 
   package id =“Microsoft.Net.Http”version =“2.2.18”targetFramework =“net45” 
   package id =“Microsoft.Web.Infrastructure”version =“1.0.0.0”targetFramework =“net45” 

有谁知道可能是什么问题? 有关如何解决它的任何想法?

编辑

样本控制器代码:




公共类ImportController
    inheritanceApiController

    
    
    公共函数SaveImportSession(sessid As string,entity As SIImportSessionDTO)As HttpResponseMessage

        尝试
             InspectionData.SaveImportSession(sessid,entity)
            昏暗的响应= Request.CreateResponse(HttpStatusCode.Accepted)
            回复
         Catch ex As HttpResponseException
            扔
        赶上前例外
            抛出新的HttpResponseException(Request.CreateResponse(Of String)(HttpStatusCode.InternalServerError,ex.Message))
        结束尝试
    结束function

我只使用基于属性的路由,这是我的webapi路由配置

公共共享子寄存器(ByVal config As HttpConfiguration)

         config.MapHttpAttributeRoutes()

         config.Routes.MapHttpRoute(_
             name:=“DefaultApi”,_
             routeTemplate:=“api / {controller} / {id}”,_
             defaults:= New with {。id = RouteParameter.Optional} _

问题是,我的服务器端代码甚至没有达到,管道上的其他东西抛出exception

谢谢

我发现了问题。

我的post代码使用$ .ajax(…),如初始消息中所示。

但是jquery的.ajax将数据发布为application / x-www-form-urlencoded。 这在以前的WebAPI版本中有效,但在最新的更新中,它似乎默认不接受application / x-www-form-urlencoded。

我将内容类型更改为application / json,我不得不将数据转换为json,这解决了问题。

所以,而不是

 j$.ajax({ type: "POST", url: uri, data: dataObj, success: function(...) 

我不得不改为

 j$.ajax({ type: "POST", url: uri, data: JSON.stringify(dataObj), contentType: "application/json; charset=utf-8", success: function(...) 

第一条线索:

从web.config的身份validation设置中删除enableCrossAppRedirects =“true”解决了我的问题。 感谢@ kiran-challa

好的,我找到了解决这个问题而没有解决方法。

请参阅此主题:http:/ / aspnetwebstack。 codeplex。 COM /讨论/ 524616

这是5.1中的一个错误 – 它在夜间构建中得到解决请参阅: https : //aspnetwebstack.codeplex.com/并特别签名每晚构建:

“每晚构建可以与Visual Studio 2010 SP1或Visual Studio 2012一起使用。要使用每晚构建:1。在您的程序包管理器设置中添加以下程序包源:….
2.添加或更新到最新版本的ASP.NET。 每晚构建标记(每晚YYYY MMM DD)。 “

这为我修复了post问题(不再需要使用JSON.stringify),还修复了上传文件的模块的多部分发布。

希望这有助于其他人! 希望很快就会有适当的补丁。

今天发布了针对此问题的修复程序。 将您的软件包更新到最新版本(5.1.1),您应该很高兴。

对不起,我暂时不能发表评论所以我必须补充一个答案。

更新到5.1后,我有完全相同的问题。

通过进行类似的更改(或实际上通过指定contentType),我能够通过此错误,但是,现在控制器方法接收到一个空对象。

这影响了应用程序中的所有POSTS – 所有以前都在工作。