首页 > 解决方案 > 嵌套值对象为空

问题描述

我有一个发送到我的 Web Api 的对象,一旦它到达我的 WebAPI,嵌套对象为空(问题在于 gatewayRoles)

服务.ts

processRequest(request: IRequest) {
  var result;
  var objectToSend = JSON.stringify(request);
  var headers = new Headers();
  headers.append('Content-Type', 'application/json');
  console.log('object to send ' + objectToSend);
  return this._http.post(environment.BASE_API_URL + "XXXX", objectToSend, {
      headers: headers
    })
    .map((res: Response) => res.json())
}

我要求

import { IGatewayRole } from './gateway-role';

export interface IRequest {
  req_id: number;
  req_type_id: number;
  gatewayRole: IGatewayRole[];
}

网关角色

export interface IGatewayRole {
  id: number;
  name: string;
}

JSON

{
  "req_id": 0,
  "req_type_id": 1,
  "gatewayRole": {
    "id": XXXXX,
    "name": "XXXXXXX"
  }
}

webAPI(数据正确进入webapi,只有网关角色未映射,值为空

public class Request {
  public int req_id {
    get;
    set;
  }
  public int req_type_id {
    get;
    set;
  }
  public List < IGatewayRole > gatewayRole = new List < IGatewayRole > ();

}

public class IGatewayRole {

  public int id {
    get;
    set;
  }
  public string name {
    get;
    set;
  }

}

[HttpPost]
[Route("API/XXXXXX/")]
[AcceptVerbs()]
public HttpResponseMessage ProcessRequest(Request val) {

  try {
    sp_processRequestResult result = null; //

    return Request.CreateResponse(HttpStatusCode.OK, _modelFactory.Create(result));
  } catch (Exception ex) {
    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
  }
}

标签: angulartypescriptasp.net-web-apiasp.net-web-api2angular-services

解决方案


推荐阅读