【问题标题】:TypeScript POST API request error 400 - Bad RequestTypeScript POST API 请求错误 400 - 错误请求
【发布时间】:2022-01-04 13:14:51
【问题描述】:

我已经设法用 HTTP Post error responed 400 解决了一个“相当简单”的问题 -> 虽然路由是 没有调用我的后端函数很好,我的要求也很好,所以对于未来的我,我想在这里发布这个问题的解决方案。

【问题讨论】:

    标签: c# html typescript post .net-core


    【解决方案1】:

    如果您从前端发布到 .Net 后端并发送对象数组,例如:

    myMethod(cars: Car[]): angular.IPromise<string[]> {
        return this.uiBackendHttp
            .post('api/controller/Method', {
                Cars: cars
            });
    }
    

    其中Car 模型包含属性,例如VehicleId、重量、型号

    不能指望后端有很多汽车

        [HttpPost]
        public async Task<string[]> Method(GetCarParameters param, CancellationToken token)
        {
            return (await _myService
                    .MyBackendFunction(Cars[], token)).[//LINQ]
    
        }
    

    你必须使用[DataContract]模型类:

    [DataContract]
    public class GetCarParameters
    {
        [DataMember]
        [Required]
        public Car[] Cars { get; set; }
    }
    

    然后你的 POST 应该是这样的:

        [HttpPost]
        public async Task<string[]> Method(GetCarParameters param, CancellationToken token)
        {
            return (await _myService
                    .MyBackendFunction(param.Cars, token)).[//LINQ]
    
        }
    

    希望对你也有帮助!

    //编辑: 没有 DataContract 它也可以工作:)

    public class GetCarParameters
    {
        public Car[] Cars { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      相关资源
      最近更新 更多