如何将控制器配置为 return null 而不是 Guid.Empty?
How to configure controller to return null instead of Guid.Empty?
为了让我的 ASP.Net Core 2.2 API 前端开发人员的生活更轻松,我希望他们收到 "null" 而不是 Guid.Empty 值(在 .净核心为“00000000-0000-0000-0000-000000000000”)。
通过像这样创建 dto 来实现此行为并不复杂:
public class ExampleDto
{
public Guid? Id { get; set; }
}
然后像这样映射它们
public static void MapExampleEntity(this AutoMapperProfile profile)
{
profile.CreateMap<ExampleEntity, ExampleDto>()
.ForMember(dto => dto.Id, o => o.MapFrom(e => e.Id == Guid.Empty ? null : e.Id);
}
然而,对于我认为控制器应该能够通过一些配置处理的任务来说,这是很多代码重复。是否可以默认实现此行为?
编辑以进一步说明我的问题(我收到了几个可行的解决方案,但我不觉得其中任何一个是实现我想做的事情的 100% 干净的方法):
我知道一般的做法是保留空值,这也是我不想用空值污染我的代码的原因。我想以这种形式保留我的 dtos:
public class ExampleDto
{
public Guid Id { get; set; }
}
而不是这种形式:
public class ExampleDto
{
public Guid? Id { get; set; }
}
避免无限的 HasValue() 检查。 Hovewer,我认为它比 return 这个(见 customerId):
{
"Id": "b3431f4d-ef87-4fb5-83e0-995a9e7c9f6a",
"Name": "A001",
"Description": null,
"Currency": null,
"CustomerId": null,
"Customer": null,
"CountryIso": 2
}
比这个:
{
"Id": "b3431f4d-ef87-4fb5-83e0-995a9e7c9f6a",
"Name": "A001",
"Description": null,
"Currency": null,
"CustomerId": "00000000-0000-0000-0000-000000000000",
"Customer": null,
"CountryIso": 2
}
这是当前行为。这些就是我想让 Guid.Empty => 空映射执行控制器而不是 AutoMapper 的原因(如果这种行为是可能的)。
Guid 是一种您已经知道的值类型。如果您想发送 null 而不是 Empty Guid,您可以检查 ActualCustomerId == Guid.IsEmpty 是否正确,如果为真,则 CustomerId = null。否则如您所说,可空类型是一种可行的方法。如果这不是您想要的,我们深表歉意。这意味着您需要检查 Guid 是否为空,如果为空则将 null 设置为 CustomerId。
试试:
public class ExampleDto
{
public Guid? Id { get{ return Id == Guid.Empty ? null : Id; } set; }
}
好吧,不知道为什么我在问这里之前没有找到这个解决方案,但我觉得把它写在这里(因为我已经问过这个问题)对于可能面临同样问题的未来开发人员来说是合适的.
确实可以在不手动检查每个 Dto 中的值或将 AutoMapper 行添加到其映射配置的情况下格式化控制器输出!
首先像这样从 JsonConverted 创建 class:
public class NullGuidJsonConverter : JsonConverter<Guid>
{
public override void WriteJson(JsonWriter writer, Guid value, JsonSerializer serializer)
{
writer.WriteValue(value == Guid.Empty ? null : value.ToString());
}
public override Guid ReadJson(JsonReader reader, Type objectType, Guid existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var value = reader.Value.ToString();
return reader.Value == null ? Guid.Empty : Guid.Parse(value);
}
}
然后将此 class 添加到 Startup.cs
中的 Mvc 设置
services.AddMvc()
.AddJsonOptions(options =>
options.SerializerSettings.Converters.Insert(0, new NullGuidJsonConverter()))
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
就是这样!控制器现在正在处理从 Guid.Empty 值到 null 的转换!
为了让我的 ASP.Net Core 2.2 API 前端开发人员的生活更轻松,我希望他们收到 "null" 而不是 Guid.Empty 值(在 .净核心为“00000000-0000-0000-0000-000000000000”)。
通过像这样创建 dto 来实现此行为并不复杂:
public class ExampleDto
{
public Guid? Id { get; set; }
}
然后像这样映射它们
public static void MapExampleEntity(this AutoMapperProfile profile)
{
profile.CreateMap<ExampleEntity, ExampleDto>()
.ForMember(dto => dto.Id, o => o.MapFrom(e => e.Id == Guid.Empty ? null : e.Id);
}
然而,对于我认为控制器应该能够通过一些配置处理的任务来说,这是很多代码重复。是否可以默认实现此行为?
编辑以进一步说明我的问题(我收到了几个可行的解决方案,但我不觉得其中任何一个是实现我想做的事情的 100% 干净的方法):
我知道一般的做法是保留空值,这也是我不想用空值污染我的代码的原因。我想以这种形式保留我的 dtos:
public class ExampleDto
{
public Guid Id { get; set; }
}
而不是这种形式:
public class ExampleDto
{
public Guid? Id { get; set; }
}
避免无限的 HasValue() 检查。 Hovewer,我认为它比 return 这个(见 customerId):
{
"Id": "b3431f4d-ef87-4fb5-83e0-995a9e7c9f6a",
"Name": "A001",
"Description": null,
"Currency": null,
"CustomerId": null,
"Customer": null,
"CountryIso": 2
}
比这个:
{
"Id": "b3431f4d-ef87-4fb5-83e0-995a9e7c9f6a",
"Name": "A001",
"Description": null,
"Currency": null,
"CustomerId": "00000000-0000-0000-0000-000000000000",
"Customer": null,
"CountryIso": 2
}
这是当前行为。这些就是我想让 Guid.Empty => 空映射执行控制器而不是 AutoMapper 的原因(如果这种行为是可能的)。
Guid 是一种您已经知道的值类型。如果您想发送 null 而不是 Empty Guid,您可以检查 ActualCustomerId == Guid.IsEmpty 是否正确,如果为真,则 CustomerId = null。否则如您所说,可空类型是一种可行的方法。如果这不是您想要的,我们深表歉意。这意味着您需要检查 Guid 是否为空,如果为空则将 null 设置为 CustomerId。
试试:
public class ExampleDto
{
public Guid? Id { get{ return Id == Guid.Empty ? null : Id; } set; }
}
好吧,不知道为什么我在问这里之前没有找到这个解决方案,但我觉得把它写在这里(因为我已经问过这个问题)对于可能面临同样问题的未来开发人员来说是合适的.
确实可以在不手动检查每个 Dto 中的值或将 AutoMapper 行添加到其映射配置的情况下格式化控制器输出!
首先像这样从 JsonConverted 创建 class:
public class NullGuidJsonConverter : JsonConverter<Guid>
{
public override void WriteJson(JsonWriter writer, Guid value, JsonSerializer serializer)
{
writer.WriteValue(value == Guid.Empty ? null : value.ToString());
}
public override Guid ReadJson(JsonReader reader, Type objectType, Guid existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var value = reader.Value.ToString();
return reader.Value == null ? Guid.Empty : Guid.Parse(value);
}
}
然后将此 class 添加到 Startup.cs
中的 Mvc 设置services.AddMvc()
.AddJsonOptions(options =>
options.SerializerSettings.Converters.Insert(0, new NullGuidJsonConverter()))
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
就是这样!控制器现在正在处理从 Guid.Empty 值到 null 的转换!