Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper uri to string convention

Tags:

automapper

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.

like image 302
DanP Avatar asked Feb 22 '26 01:02

DanP


1 Answers

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;
    }
}
like image 89
BeRecursive Avatar answered Feb 26 '26 03:02

BeRecursive



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!