Automapper uri to string convention

Using Automapper, what is the best way to set up a global convention so that all properties are System.Uriconverted to a string that represents the property AbsoluteUri?

Ideally, I would like null System.Uri to resolve a value String.Empty, not null.

+3
source share
1 answer

Set up the map:

Mapper.CreateMap<System.Uri, string>().ConvertUsing<UriToStringConverter>();

Create a 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;
    }
}
+7
source

Source: https://habr.com/ru/post/1773740/


All Articles