我正在我的 Angular 服务上使用 asp.net、mvc6 和 angularjs 开发应用程序。当我向操作方法发出请求时,我没有收到任何传递的数据。 当我检查请求时,我可以看到由以下原因引起的异常:
- Form '((Microsoft.AspNet.Http.Internal.DefaultHttpRequest)this.Request).Form' threw an exception of type 'System.InvalidOperationException' Microsoft.AspNet.Http.IFormCollection {System.InvalidOperationException}
异常消息说 "Incorrect Content-Type:application/json;charset=UTF-8"
我的 angular.service
return $http({ method: 'POST', url: 'home/createEvent', eventDetails: event })
.success(function(data, status, headers, config) {
return data;
})
.catch(function(data, status, headers, config) {
console.log(data);
});
在我的 Controller 上
[HttpPost]
public IActionResult CreateEvent([FromBody]Event eventDetails)
{
return Json(new {dsd=""},
new JsonSerializerSettings {ContractResolver = new CamelCasePropertyNamesContractResolver()});
}
最佳答案
接受的答案对我不起作用。对我来说,解决方案是向 $http 调用添加 header :
$http({
url: "/install",
method: "POST",
data: data,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
})
.success(...);
关于c# - "Incorrect Content-Type: "异常抛出 Angular mvc 6 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33711629/