Angular httpClient - POST 成功,没有发送参数

Angular httpClient - POST successful, no parameters sent

我正在尝试在我的 Ionic4 应用程序中做一些非常基本的事情 - post 数据到 Asp.Net Web API 2 界面。请求成功,但服务器没有收到数据

import { HttpClient } from '@angular/common/http';

someAction(assetId: number) {
let asset = new FormData();
asset.append("assetId", assetId.toString());
asset.append("UserId", "1");

return this.httpClient.post(this.url + "SomeAction", asset, { headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } }).toPromise();
    }

在服务器上 (C#):

[HttpPost]
[Route("Service/SomeAction")]
public HttpResponseMessage SomeAction(AccessData asset)
{
  return new HttpResponseMessage(_service.LogAsset(asset));
}

服务器上的assetobject已实例化,但不包含客户端发送的值。

此外,removing/changing header 发送导致请求完全失败。

编辑: 删除 header 导致错误 404,并且在控制台中也有这个:

Access to XMLHttpRequest at 'http://server' from origin 'http://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

将 header 更改为 "multipart/form-data" 导致错误 415 - 不支持的媒体类型

编辑#2: 这适用于 Postman 和 HTTPBot (iPhone),只是不适用于我的 Ionic 应用程序

如删除 Content-Type header 时的错误中所述:

Access to XMLHttpRequest at 'http://server' from origin 'http://localhost' has been blocked by CORS policy

我转而使用 Ionic native http plugin as a solution. Which is not the end of my problems, though. See this 了解更多信息。

更新:我最终通过启用 CORS 解决了这个问题。我在 web.config 中启用了 CORS 并期望它就足够了,但事实证明我实际上需要安装 Microsoft.AspNet.WebApi.Cors 包并将 [EnableCors] 属性添加到我的 post 方法。