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