Using Automapper, what is the best way to setup a global convention such that all System.Uri properties are converted to a string that represents the AbsoluteUri property?
Ideally, I'd like to have a null System.Uri resolve to a value of String.Empty rather than null.
Setup the map:
Mapper.CreateMap<System.Uri, string>().ConvertUsing<UriToStringConverter>();
Create the TypeConverter class:
public class UriToStringConverter : ITypeConverter<System.Uri, string>
{
public string Convert(ResolutionContext context)
{
if (context.SourceValue == null)
{
return String.Empty;
}
return ((System.Uri)context.SourceValue).AbsoluteUri;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With