更新后发送的数据来向的WebAPI 5.1.0失败 [英] POSTing data to WebApi after update to 5.1.0 fails

查看:355
本文介绍了更新后发送的数据来向的WebAPI 5.1.0失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的WebAPI加载的web应用程序/数据保存
所有用于工作确定/
今天,我发现有一个更新到ASP.NET MVC /的​​WebAPI,我安装更新
但是与jQuery的数据发布后不再工作。
我做了无论是在客户端code或控制器的WebAPI没有变化。

I have an web app that use webapi for loading / saving data All used to work ok/ Today I found that there is an updated to ASP.NET MVC / webapi, and I installed the update But after that POSTing data with jquery no longer works. I made no change either in client code or in webapi controller.

我的典型的后语句看起来如下:

My typical POSt statement looks as below:

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 + '<br>' + error, true);
    },
    success: function (data, status, jqXHR) {
        showProgress(false);
        if (status === 'success') {
        GLOBAL.setMessage(self, opType + " saved ok");
        //....

在$阿贾克斯code得到以下错误

The $.ajax code gets the following error


"An error has occurred."

ExceptionMessage
    "This method or property is not supported after HttpRequest.Form, Files, InputStream, or BinaryRead has been invoked."

ExceptionType
    "System.Web.HttpException"

StackTrace
at System.Web.HttpRequest.GetInputStream(Boolean persistEntityBody, Boolean disableMaxRequestLength) 
at System.Web.HttpRequestWrapper.GetBufferedInputStream() 
at System.Web.Http.WebHost.SeekableBufferedRequestStream..ctor(HttpRequestBase request) 
at System.Web.Http.WebHost.HttpControllerHandler.c__DisplayClass11.b__b() at System.Web.Http.WebHost.HttpControllerHandler.LazyStreamContent.get_StreamContent() at System.Web.Http.WebHost.HttpControllerHandler.LazyStreamContent.CreateContentReadStreamAsync() at System.Net.Http.HttpContent.ReadAsStreamAsync() at System.Net.Http.HttpContentExtensions.d__0`1.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
at System.Web.Http.ModelBinding.FormatterParameterBinding.d__0.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter.GetResult() 
at System.Web.Http.Controllers.HttpActionBinding.d__0.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
at System.Web.Http.Filters.AuthorizationFilterAttribute.d__2.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()"

以上是包的NuGet从我的解决方案

These are the nuget packages from my solution


  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" 

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

Does anyone knows what could be the problem? Any idea on how to fix it?

修改

样控制器code:





Public Class ImportController
    Inherits ApiController

    
    
    Public Function SaveImportSession(sessid As string,  entity As SIImportSessionDTO) As HttpResponseMessage

        Try
            InspectionData.SaveImportSession(sessid, entity)
            Dim response = Request.CreateResponse(HttpStatusCode.Accepted)
            Return response
        Catch ex As HttpResponseException
            Throw
        Catch ex As Exception
            Throw New HttpResponseException(Request.CreateResponse(Of String)(HttpStatusCode.InternalServerError, ex.Message))
        End Try
    End Function

我只使用基于属性的路线,这是我的WebAPI航线的配置

I'm using only attribute-based routes, and this is my webapi route config


Public Shared Sub Register(ByVal config As HttpConfiguration)

        config.MapHttpAttributeRoutes()

        config.Routes.MapHttpRoute( _
            name:="DefaultApi", _
            routeTemplate:="api/{controller}/{id}", _
            defaults:=New With {.id = RouteParameter.Optional} _

问题,甚至没有达到我的服务器端code,别的东西在管道上引发异常

The problem, my server side code is not even reached, something else on the pipeline throw the exception

感谢

推荐答案

我发现这个问题。

我的岗位code $使用阿贾克斯(...),如图初始消息。

My post code uses $.ajax(...), as shown in initial message.

但jQuery的阿贾克斯后的数据作为应用程序/ x-WWW的形式urlen codeD。
这个曾在的WebAPI的previous版本,但在最新的更新似乎并不接受默认的应用程序/ x-WWW的形式urlen codeD。

But jquery's .ajax post data as application/x-www-form-urlencoded. This worked in previous versions of WebAPI, but in latest update it seems it doesn't accept application/x-www-form-urlencoded by default.

我改变内容类型应用程序/ JSON和我只好把数据转换为JSON,这解决了这个问题。

I changed content type to application/json, and I had to convert data to json, and this solved the problem.

因此​​,代替

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(...)

这篇关于更新后发送的数据来向的WebAPI 5.1.0失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆