过滤 ASP.NET Web API 中的响应字段

Filtering the response fileds in ASP.NET Web API

我正在使用 EF 构建 WEB API。我想在显示响应时过滤 table 的列。由于 table 中有许多不需要的字段,因此序列化内容时出现问题。

SQL Table 的设计,API 的建造地点:

CREATE TABLE [dbo].[flight_new](
    [flight_id] [uniqueidentifier] NOT NULL,
    [utc_departure_date] [datetime] NULL,
    [airline_rcd] [varchar](20) NULL,
    [flight_number] [varchar](20) NULL,
    [flight_status_rcd] [varchar](20) NULL,
    [aircraft_type_rcd] [varchar](20) NULL,
    [matriculation_rcd] [nvarchar](60) NULL,
    [operating_airline_rcd] [varchar](20) NULL,
    [operating_flight_number] [varchar](20) NULL,
    [origin_rcd] [varchar](20) NULL,
    [destination_rcd] [varchar](20) NULL,
    [leg_inventory_flag] [tinyint] NULL,
    [flight_comment] [nvarchar](450) NULL,
    [internal_comment] [nvarchar](450) NULL,
    [controlling_agency_code] [varchar](20) NULL,
    [open_book_date_time] [datetime] NULL,
    [open_book_by] [uniqueidentifier] NULL,
    [cancelled_date_time] [datetime] NULL,
    [cancelled_by] [uniqueidentifier] NULL,
    [locked_by] [uniqueidentifier] NULL,
    [locked_date_time] [datetime] NULL,
    [create_by] [uniqueidentifier] NULL,
    [create_date_time] [datetime] NULL,
    [update_by] [uniqueidentifier] NULL,
    [update_date_time] [datetime] NULL,
    [schedule_id] [uniqueidentifier] NULL,
    [free_seating_flag] [tinyint] NULL,
    [auto_open_checkin_flag] [tinyint] NULL,
    [allow_web_checkin_flag] [tinyint] NULL,
    [flight_information_1] [nvarchar](60) NULL,
    [flight_information_2] [nvarchar](60) NULL,
    [flight_information_3] [nvarchar](60) NULL,
    [activated_by] [uniqueidentifier] NULL,
    [activated_date_time] [datetime] NULL,
    [inactivated_by] [uniqueidentifier] NULL,
    [inactivated_date_time] [datetime] NULL,
    [exclude_statistics_flag] [tinyint] NULL,
    [dot_reporting_date_time] [datetime] NULL
) ON [PRIMARY]

GO

我创建了一个新的 class 用于将必填字段显示为,但我只想将响应显示为:

  private class ExpectedResponse
        {
            public DateTime? utc_departure_date { get; set; }
            public string flight_number { get; set; }
            public string origin_rcd { get; set; }
            public string destination_rcd { get; set; }
            public string flight_status_rcd { get; set; }
        }

获取响应的控制器方法如下。

    // GET api/FlightInfo
//Default Method
    public IQueryable<ExpectedResponse> Getflight_new()
    {
        return db.flight_new
         .ToList()
         .Select(p => new ExpectedResponse
         {
             utc_departure_date = p.utc_departure_date,
             flight_number = p.flight_number,
             origin_rcd = p.origin_rcd,
             destination_rcd = p.destination_rcd,
             flight_status_rcd = p.flight_status_rcd,
         })
         .AsQueryable();
    }

    private class ExpectedResponse
    {
        public DateTime? utc_departure_date { get; set; }
        public string flight_number { get; set; }
        public string origin_rcd { get; set; }
        public string destination_rcd { get; set; }
        public string flight_status_rcd { get; set; }
    }

构建时出现错误:'System.Linq.IQueryable<YCFAPI.Controllers.FlightInfoController.ExpectedResponse>' is less accessible than method 'YCFAPI.Controllers.FlightInfoController.Getflight_new()'

如何获得所需的响应。非常感谢。

在此处将private更改为public

public class ExpectedResponse
{
  public DateTime? utc_departure_date { get; set; }
  public string flight_number { get; set; }
  public string origin_rcd { get; set; }
  public string destination_rcd { get; set; }
  public string flight_status_rcd { get; set; }
}

我会将 ExpectedResponse 移动到另一个文件,例如模型文件夹中的 ExpectedResponse.cs。只是为了提供一个干净的 MVC 结构,以便 Controller 执行所有逻辑,而 Models 保存数据,获取数据或定义特定数据的属性的外观