问题描述:在使用Angular发出HTTP请求时,发现无法将请求主体绑定到.NET Core WebAPI的原始参数中。
解决方法:
示例代码:
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable()
export class MyService {
constructor(private http: HttpClient) {}
sendData(data: any) {
const body = JSON.stringify(data);
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
return this.http.post('/api/myendpoint', body, { headers: headers });
}
}
示例代码:
[HttpPost]
public IActionResult MyEndpoint([FromBody] string requestBody)
{
// 处理请求主体
return Ok();
}
注意事项:
这些步骤将确保将请求主体正确绑定到.NET Core WebAPI的原始参数中。