要在Angular 7中发送NULLS到.NET Core Web API,您可以按照以下步骤进行操作:
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ApiService {
apiUrl = 'http://api.example.com/';
constructor(private http: HttpClient) {}
postData(data: any) {
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
// 使用JSON.stringify将数据转换为JSON字符串
const body = JSON.stringify(data);
// 发送HTTP POST请求
return this.http.post(this.apiUrl + 'endpoint', body, { headers: headers });
}
}
import { Component } from '@angular/core';
import { ApiService } from './api.service';
@Component({
selector: 'app-root',
template: `
`
})
export class AppComponent {
constructor(private apiService: ApiService) {}
sendData() {
const data = {
value: null
};
this.apiService.postData(data).subscribe(response => {
console.log(response);
});
}
}
[HttpPost("endpoint")]
public IActionResult PostData([FromBody] MyModel model)
{
// 执行操作并返回结果
return Ok();
}
public class MyModel
{
public object Value { get; set; }
}
这样,您就可以在Angular 7中发送NULLS到.NET Core Web API了。