gpt4 book ai didi

angular - 从 Angular 发送数据时,web-api POST 主体对象始终为 null

转载 作者:可可西里 更新时间:2023-11-01 17:16:42 25 4
gpt4 key购买 nike

<分区>

我有一个 .Net Core 2.2 Web api 正在监听 Angular 前端。我从 Angular 服务向后端发送 JSON 数据,我在 chrome 开发工具和 fiddler 中检查数据,所以我很清楚发送的是什么。正在命中端点,但正文未解析,我的后端认为它为空。我看过堆栈溢出的人遇到的类似问题,但他们的解决方案似乎都不起作用(FormBody、更改发送的数据等)。这是我来自 fiddler 的数据包、我的前端代码和我的 webAPI/viewmodel。我在我的 C# 端点中放置了一个断点,它被击中,但“testInstance”对象始终为空。知道这是为什么,也许是字符编码?

fiddler 数据

原始:

POST https://localhost:44380/api/Shipping/shippingDoc HTTP/1.1
Host: localhost:44380
Connection: keep-alive
Content-Length: 16
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/plain, */*
Origin: https://localhost:44380
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36
Content-Type: application/json; charset=UTF-8
Referer: https://localhost:44380/dataentry
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

{"name":"hello"}

TextView :

{"name":"hello"}

Angular 8 前端服务:

@Injectable({
providedIn: 'root'
})
export class ShippingService {

private shippingURL : string = 'api/Shipping/shippingDoc';
constructor(private http: HttpClient) { }

postShippingDocForm(shippingDoc : ShippingDoc) : Observable<any> {
var headers = new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8');
var test = {name: "hello"} //dummy data
return this.http.post(this.shippingURL, test, {headers: headers});
}
}

.Net核心网络接口(interface):

namespace TrackingSystem.Controllers
{
[Route("api/Shipping")]
public class ShippingController : ControllerBase
{

[HttpPost("shippingDoc")]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public ActionResult<Views.Shipping.Test> CreateShippingDocForm(Views.Shipping.Test testInstance)
{
return testInstance; //breakpoint is here
}

}
}

.Net核心 View 模型:

namespace TrackingSystem.Views.Shipping
{
public class Test
{
public string name { get; set; }
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com