angularjs - 该资源不支持请求实体的媒体类型 'application/x-www-form-urlencoded'

标签 angularjs asp.net-mvc asp.net-web-api data-formatters aspnetboilerplate

我正在使用 ASPNET Boilerplate 框架来开发我的应用程序,但我确信这是一个 WebAPI 特定问题。我正在使用基于以下应用程序层函数构建的动态 WebAPI 函数。

Application Service function

在我的 Angular 应用程序上,我使用以下代码将数据 POST 到动态 API 并最终发送到应用程序层函数。

var formDataObject = new FormData();
                formDataObject.append('avatar', vm.group.avatar);
                formDataObject.append('name', vm.group.name);
                formDataObject.append('description', vm.group.description);
                
                $.ajax({
                    url: abp.appPath + 'api/services/app/group/CreateGroup',
                    processData: false,
                    //contentType: false,
                    type: 'POST',
                    data: formDataObject
                });

我在 Chrome 控制台中收到以下错误:

{"message":"The request entity's media type 'application/x-www-form-urlencoded' is not supported for this resource.","exceptionMessage":"No MediaTypeFormatter is available to read an object of type 'InputGroup' from content with media type 'application/x-www-form-urlencoded'.","exceptionType":"System.Net.Http.UnsupportedMediaTypeException","stackTrace":"   at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"}

我尝试了以下解决方案,但没有成功:

  • 使用 FormDataCollection 作为参数类型。
  • 发布时不包含文件(头像)对象。失败。
  • 使用应用程序/表单数据类型发布。失败。

我在互联网上看到的所有解决方案都不适合我。

最佳答案

好吧,经过几个小时的调试我解决了这个问题,所以如果其他人面临同样的问题可以节省时间。这不是 WebAPI 问题。 WebAPI 为“application/x-www-form-urlencoded”提供 MediaTypeFormatters。此问题是由于 Abp.WebApi 包的某些实现已过时造成的。将您的 aspnet 样板包更新到 2016 年 9 月 28 日发布的 Abp v1.0。修复损坏的代码。并重新编译。这个问题应该得到解决。如果您仍然遇到问题,请联系我,也许我可以提供帮助。

关于angularjs - 该资源不支持请求实体的媒体类型 'application/x-www-form-urlencoded',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39825388/

相关文章:

c# - 如何将二进制数据转换为图片显示在div标签或img标签中?

html - itextsharp "razor to html"工作正常但 "to pdf"忽略样式

javascript - 将数据传递给 Bootstrap 模式

javascript - 在 Angular 中通过 $scope 安全地使用 eval()

angularjs - 如何在 ionic 框架中获得 <ion-input> 的值?

asp.net - 如何在.Net Web API中包含System.Web.Http.Cors引用dll

asp.net - Angular2 HTTP 后 ASP.NET MVC Web API

azure - Swagger 注销按钮不会从浏览器清除用户 session (ASP.Net 6 Web api 中的 Azure B2C)

javascript - AngularJS/CSS - 根据文本的值改变文本的颜色

javascript - 使用 AngularJS Satellizer 模块的 Linkedin 身份验证