const data = { name: 'John', age: 30 }; const headers = { 'Content-Type': 'application/json' }; this.http.post('https://example.com/api/user', data, { headers }).subscribe(response => { console.log(response); });
[Route("api/user")] [ApiController] public class UserController : ControllerBase { [HttpPost] public IActionResult Create([FromBody] User user) { // 从POST请求中获取到的JSON数据被绑定到user对象中 // 处理user对象,返回创建结果 } }
app.Use(async (context, next) => { context.Request.EnableBuffering(); // 允许中间件读取请求体 await next.Invoke(); });
app.UseRequestBodyBuffer(); // 禁用.NET Core的RequestBody重用功能
app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); });