Asp.net MVC??ζ???????????????????????Trim????
????????п? ???????[ 2017/6/21 10:05:53 ] ??????????????????? .NET ?????
???????????????????????????????????????ж?????Trim???????????ViewModel????θ????????????????????????????鷢????????????????????
????MVC4.6???????
????1?????IModelBinder?????????????ModelBinder??
????public class TrimModelBinder : IModelBinder
????{
????public object BindModel(ControllerContext controllerContext?? ModelBindingContext bindingContext)
????{
????var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
????string attemptedValue = valueResult?.AttemptedValue;
????return string.IsNullOrWhiteSpace(attemptedValue) ? attemptedValue : attemptedValue.Trim();
????}
????}
????2?????ModelBinder??MVC?????
????protected void Application_Start()
????{
????//System.Web.Mvc.ModelBinders.Binders.DefaultBinder = new ModelBinders.TrimModelBinder();
????System.Web.Mvc.ModelBinders.Binders.Add(typeof(string)?? new ModelBinders.TrimModelBinder());
????AreaRegistration.RegisterAllAreas();
????FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
????RouteConfig.RegisterRoutes(RouteTable.Routes);
????BundleConfig.RegisterBundles(BundleTable.Bundles);
????}
????3????????Ч??
??????????????????Trim?????????ViewModel???????1???
????Asp.net core 1.1 MVC???????
????1???????ModelBinder?????ComplexTypeModelBinder
????public class TrimModelBinder : ComplexTypeModelBinder
????{
????public TrimModelBinder(IDictionary propertyBinders) : base(propertyBinders) { }
????protected override void SetProperty(ModelBindingContext bindingContext?? string modelName?? ModelMetadata propertyMetadata?? ModelBindingResult result)
????{
????var value = result.Model as string;
????result= string.IsNullOrWhiteSpace(value) ? result : ModelBindingResult.Success(value.Trim());
????base.SetProperty(bindingContext?? modelName?? propertyMetadata?? result);
????}
????}
????2???ModelBinder????????Provider??
????public class TrimModelBinderProvider : IModelBinderProvider
????{
????public IModelBinder GetBinder(ModelBinderProviderContext context)
????{
????if (context.Metadata.IsComplexType && !context.Metadata.IsCollectionType)
????{
????var propertyBinders = new Dictionary();
????for (int i = 0; i < context.Metadata.Properties.Count; i++)
????{
????var property = context.Metadata.Properties[i];
????propertyBinders.Add(property?? context.CreateBinder(property));
????}
????return new TrimModelBinder(propertyBinders);
????}
????return null;
????}
????}
????3????Provider???????????
????services.AddMvc().AddMvcOptions(s =>
????{
????s.ModelBinderProviders[s.ModelBinderProviders.TakeWhile(p => !(p is ComplexTypeModelBinderProvider)).Count()] = new TrimModelBinderProvider();
????});
????4????????Ч??
??????????????????Trim?????????ViewModel???????1???
???????
???????????????????????.
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11