.NET WEB API2 从 JSON 输出中的子 class 检索字段
.NET WEB API2 Retrieve a field from the child class in JSON output
我正在使用 C# 开发 MVC5 Web API 2 应用程序,我需要帮助格式化 JSON 消息。
目前我的 GET/Appointments 我得到这个 JSON 结果:
[
{
"Id": 1,
"FullName": "John Doe",
"Store": {
"Id": 4,
"Name": "Acme Store Inc.",
"StoreType": {
"Value": "Big Box Store"
}
}
},
{
...
}
]
如何更改我的代码以使 JSON 调用看起来像这样?
[
{
"Id": 1,
"FullName": "John Doe",
"Store": {
"Id": 4,
"Name": "Acme Store Inc.",
"Value": "Big Box Store"
}
},
{
...
}
]
这是我的设置:
Models\Appointment.cs
public class Appointment
{
public int Id { get; set; }
public string FullName{ get; set; }
public Store Store { get; set; }
public int StoreId { get; set; }
}
Models\Store.cs
public class Store
{
public int Id { get; set; }
public string Name { get; set; }
public StoreType StoreType { get; set; }
public int StoreTypeId { get; set; }
}
Models\StoreType.cs
public class CenterType
{
public int Id { get; set; }
public string Value { get; set; }
}
Models\MyDBContext.cs
public class MyDBContext:DbContext
{
public DbSet<StoreType> StoreTypes { get; set; }
public DbSet<Store> Stores { get; set; }
public DbSet<Appointment> Appointments { get; set; }
public MyDBContext():base("MyTestDatabase")
{
}
}
App_Start\MappingProfile.cs
public class MappingProfile:Profile
{
public MappingProfile()
{
Mapper.CreateMap<Appointment, AppointmentDto>();
Mapper.CreateMap<Store, StoreDto>();
Mapper.CreateMap<StoreType, StoreTypeDto>();
}
}
Controllers\API\AppointmentsController.cs
public class AppointmentsController : ApiController
{
private MyDBContext _context;
public AppointmentsController()
{
_context = new MyDBContext();
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
// GET /api/appointments
[HttpGet]
public IEnumerable<AppointmentDto> GetAppointments()
{
return _context.Appointments.Include(x => x.Store).Include(x => x.Store.StoreType).ToList().Select(Mapper.Map<Appointment, AppointmentDto>);
}
}
您需要更改 DTO 结构才能完成您想要的。我将给出一个可行的简单示例。
class AppointmentDto {
public long Id { get; set; }
public string FullName { get; set; }
public StoreDto Store { get; set; }
}
class StoreDto {
public long Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
根据 Andrew Kretzer 的建议,您的 DTO 应如下所示
class AppointmentDto {
public long Id { get; set; }
public string FullName { get; set; }
public StoreDto Store { get; set; }
}
class StoreDto {
public long Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
但是要获得预期的行为,您需要将映射更改为:
Mapper.CreateMap<Store, StoreDto>();
Mapper.CreateMap<StoreType, StoreTypeDto>();
到
Mapper.CreateMap<Store, StoreDto>()
.ForMember(x => x.Value, x => x.MapFrom(store => store.StoreType.Value));
我正在使用 C# 开发 MVC5 Web API 2 应用程序,我需要帮助格式化 JSON 消息。
目前我的 GET/Appointments 我得到这个 JSON 结果:
[
{
"Id": 1,
"FullName": "John Doe",
"Store": {
"Id": 4,
"Name": "Acme Store Inc.",
"StoreType": {
"Value": "Big Box Store"
}
}
},
{
...
}
]
如何更改我的代码以使 JSON 调用看起来像这样?
[
{
"Id": 1,
"FullName": "John Doe",
"Store": {
"Id": 4,
"Name": "Acme Store Inc.",
"Value": "Big Box Store"
}
},
{
...
}
]
这是我的设置:
Models\Appointment.cs
public class Appointment
{
public int Id { get; set; }
public string FullName{ get; set; }
public Store Store { get; set; }
public int StoreId { get; set; }
}
Models\Store.cs
public class Store
{
public int Id { get; set; }
public string Name { get; set; }
public StoreType StoreType { get; set; }
public int StoreTypeId { get; set; }
}
Models\StoreType.cs
public class CenterType
{
public int Id { get; set; }
public string Value { get; set; }
}
Models\MyDBContext.cs
public class MyDBContext:DbContext
{
public DbSet<StoreType> StoreTypes { get; set; }
public DbSet<Store> Stores { get; set; }
public DbSet<Appointment> Appointments { get; set; }
public MyDBContext():base("MyTestDatabase")
{
}
}
App_Start\MappingProfile.cs
public class MappingProfile:Profile
{
public MappingProfile()
{
Mapper.CreateMap<Appointment, AppointmentDto>();
Mapper.CreateMap<Store, StoreDto>();
Mapper.CreateMap<StoreType, StoreTypeDto>();
}
}
Controllers\API\AppointmentsController.cs
public class AppointmentsController : ApiController
{
private MyDBContext _context;
public AppointmentsController()
{
_context = new MyDBContext();
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
// GET /api/appointments
[HttpGet]
public IEnumerable<AppointmentDto> GetAppointments()
{
return _context.Appointments.Include(x => x.Store).Include(x => x.Store.StoreType).ToList().Select(Mapper.Map<Appointment, AppointmentDto>);
}
}
您需要更改 DTO 结构才能完成您想要的。我将给出一个可行的简单示例。
class AppointmentDto {
public long Id { get; set; }
public string FullName { get; set; }
public StoreDto Store { get; set; }
}
class StoreDto {
public long Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
根据 Andrew Kretzer 的建议,您的 DTO 应如下所示
class AppointmentDto {
public long Id { get; set; }
public string FullName { get; set; }
public StoreDto Store { get; set; }
}
class StoreDto {
public long Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
但是要获得预期的行为,您需要将映射更改为:
Mapper.CreateMap<Store, StoreDto>();
Mapper.CreateMap<StoreType, StoreTypeDto>();
到
Mapper.CreateMap<Store, StoreDto>()
.ForMember(x => x.Value, x => x.MapFrom(store => store.StoreType.Value));