/// <summary> /// /// </summary> /// <param name="controllerContext"></param> /// <param name="bindingContext"></param> /// <returns></returns> public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var modelName = bindingContext.ModelName; IUnvalidatedValueProvider valueProvider = bindingContext.ValueProvider as IUnvalidatedValueProvider; var valueProviderResult = valueProvider == null?bindingContext.ValueProvider.GetValue(modelName) : valueProvider.GetValue(modelName, true); if (valueProviderResult == null) { return(null); } bindingContext.ModelState.SetModelValue(modelName, valueProviderResult); string value = valueProviderResult.AttemptedValue; if (string.IsNullOrEmpty(value)) { return(null); } IXssSchemeName scheme = bindingContext.ModelMetadata?.ContainerType?.GetProperty(bindingContext.ModelMetadata.PropertyName).GetCustomAttributes(true)?.Cast <IXssSchemeName>()?.FirstOrDefault() ?? null; if (scheme == null) { return((RichText)value); } RichText richText = new RichText(value, DependencyResolver.Current.GetService <IFilterPolicyFactory>().CreateHtmlFilter(scheme.GetSchemeName(controllerContext.HttpContext).Result).Result); return(richText); }
/// <summary> /// /// </summary> /// <param name="bindingContext"></param> /// <returns></returns> public async Task BindModelAsync(ModelBindingContext bindingContext) { if (bindingContext == null) { throw new ArgumentNullException(nameof(bindingContext)); } var modelName = bindingContext.ModelName; var valueProviderResult = bindingContext.ValueProvider.GetValue(modelName); if (valueProviderResult == ValueProviderResult.None) { return; } bindingContext.ModelState.SetModelValue(modelName, valueProviderResult); string value = (string)valueProviderResult; if (string.IsNullOrEmpty(value)) { return; } IXssSchemeName scheme = bindingContext.ModelMetadata is DefaultModelMetadata defaultModelMetadata? (defaultModelMetadata.MetadataKind == ModelMetadataKind.Parameter ? defaultModelMetadata.Attributes.ParameterAttributes : defaultModelMetadata.Attributes.PropertyAttributes)?.Cast <IXssSchemeName>()?.FirstOrDefault():null; RichText richText = scheme == null ?(RichText)value : new RichText(value, await bindingContext.HttpContext.RequestServices.GetService <IFilterPolicyFactory>().CreateHtmlFilter(await scheme.GetSchemeName(bindingContext.HttpContext))); bindingContext.Result = ModelBindingResult.Success(richText); }