Automapper空字符串空 [英] Automapper null string to empty
本文介绍了Automapper空字符串空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试映射具有空字符串属性的对象,目标也为空。是否有一个全球性的设置,我可以把上说,所有的空字符串应该被映射到空的?
When I try to map an object that has a null string property, the destination is also null. Is there a global settings I can turn on that says all null string should be mapped to empty?
推荐答案
这样的事情应该工作:
public class NullStringConverter : ITypeConverter<string, string>
{
public string Convert(string source)
{
return source ?? string.Empty;
}
}
而在你的配置类:
And in your configuration class:
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.CreateMap<string, string>().ConvertUsing<NullStringConverter>();
Mapper.AddProfile(new SomeViewModelMapper());
Mapper.AddProfile(new SomeOtherViewModelMapper());
...
}
}
这篇关于Automapper空字符串空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文