Search code examples
asp.net-mvcasp.net-mvc-5

ASP.NET MVC - HtmlEncode all strings by default


For example, a user creates a new question on a forum.

I send ajax to the server, then I use HtmlEncode to exclude the HTML code before saving it in the database.

Is it possible that HtmlEncode would be used automatically when receiving a request?

Also, when using the attribute (for example [HtmlAllowed]) you can allow html code in request.

Thanks


Solution

  • Thanks to Yegor Androsov, for pointing me to right direction.

    This ModelBinder automaticaly encode all string properties, except that has [SafeHtml] attribute

    public class SafeStringModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase request = controllerContext.HttpContext.Request;
            string name = bindingContext.ModelName;
            string value = request.Unvalidated[name];
    
            Type holderType = bindingContext.ModelMetadata.ContainerType;
            if (holderType != null)
            {
                PropertyInfo propertyType = holderType.GetProperty(bindingContext.ModelMetadata.PropertyName);
                if (propertyType == null) return value;
    
                object[] attributes = propertyType.GetCustomAttributes(true);
                bool hasAttribute = attributes.Cast<Attribute>().Any(a => a.GetType() == typeof (SafeHtmlAttribute));
    
                if (!hasAttribute && !string.IsNullOrEmpty(value))
                {
                    value = HttpUtility.HtmlEncode(value);
                }
            }
    
            return value;
        }
    }
    
    [AttributeUsage(AttributeTargets.Property)]
    public class SafeHtmlAttribute : Attribute { }