I recent question on StackOverflow was how to HTML Encode characters with Glass.Mapper.

On StackOverflow Erwin asked a question about Glass not encoding text characters in Single Line Text or Multi Line Text. He is quite right, Glass treats these as pure text values. However this isn't always ideal and there will be times when you want to add characters such as < and > to your fields and have the encoded to their HTML friendly equivalent.

This is very simple to achieve with Glass.Mapper, first we need to create a custom type that will represent our encoded string:

    public class EncodedString
    {
        public string RawValue { get; set; }

        public EncodedString(string value)
        {
            RawValue = value;
        }

        public static implicit operator string(EncodedString encoded)
        {
            return encoded.ToString();
        }

        public static implicit operator EncodedString(string value)
        {
            return new EncodedString(value);
        }
        public override string ToString()
        {
            return HttpUtility.HtmlEncode(RawValue);
        }
    }

Next we need to create a data mapper that will convert the field value to this type:

    public class EncodedStringDataMapper : Glass.Mapper.Sc.DataMappers.AbstractSitecoreFieldMapper
    {
        public EncodedStringDataMapper() : base(new[] {typeof (EncodedString)})
        {
        }
        public override string SetFieldValue(object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context)
        {
            EncodedString encoded = value as EncodedString;
            return value == null ? null : encoded.RawValue;
        }

        public override object GetFieldValue(string fieldValue, SitecoreFieldConfiguration config, SitecoreDataMappingContext context)
        {
            return new EncodedString(fieldValue);
        }
    }

Finally we need to tell the IOC container that this new handlers exists, this is done in the /App_Start/GlassMapperScCustom.cs class.

    public static  class GlassMapperScCustom
    {
		public static void CastleConfig(IWindsorContainer container){
			var config = new Config();

            container.Register(Component.For<AbstractDataMapper>().ImplementedBy<EncodedStringDataMapper>().LifestyleTransient());
			container.Install(new SitecoreInstaller(config));
		}

We can now update our model to use this type instead of a standard string:

    public class Home
    {
        public virtual Guid Id { get; set; }
        public virtual EncodedString Title { get; set; }
        public virtual EncodedString Text { get; set; }
    }

Being able to get encoded strings is missing from the Glass.Mapper framework and I will be adding this to the next release.

Glass needs your support!

Glass.Mappper.Sc is supported by the generous donations of the Glass.Mapper.Sc community! Their donations help fund the time, effort and hosting for the project.

These supporters are AMAZING and a huge thank you to all of you. You can find our list of supporters on the Rockstars page.

If you use Glass.Mapper.Sc and find it useful please consider supporting the project. Not only will you help the project but you could also get a discount on the Glass.Mapper.Sc training and join the Rockstars page.

Become a Rockstar