gpt4 book ai didi

c# - REST Web 服务 - 获取和创建的不同类型

转载 作者:行者123 更新时间:2023-12-03 20:50:18 26 4
gpt4 key购买 nike

使用 ASP.Net Web Api,您的 GET 端点返回与 POST 端点接受的类型不同的类型是否正常/良好做法?

在很多情况下,我们想要在 GET 中返回的字段不能在 POST 中设置,例如“LastUpdated”、“Total”等,需要不同的类型吗?

例如,GET 返回 ReservationForGetModel,而 POST 接受 ReservationForCreateModel:

public class ReservationController : ApiController {
...

public HttpResponseMessage Get(int id) {
Reservation reservation = _reservationService.Get(id);
//map Reservation to ReservationForGetModel
//return ReservationForGetModel
}

public HttpResponseMessage Post(ReservationForCreateModel reservation) {
//create reservation using ReservationForCreateModel here
//return 201 with location header set
}

}

最佳答案

GET 和 POST 可以使用完全不同的媒体类型。考虑 HTML 表单:您使用 application/x-www-urlencoded-form 进行 POST,GET 返回 text/html

对于 GET 和 PUT,媒体类型更有可能是对称的,但这不是硬性规定。

关于c# - REST Web 服务 - 获取和创建的不同类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20056785/

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