效果图:

01

02

直接贴代码了:

NoTrim

    public class NoTrimAttribute : Attribute
{
}

我们自定义的模型绑定提供程序

    /// <summary>
/// 自定义的“天空”模型绑定提供程序
/// </summary>
public class MyModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context)); if (!context.Metadata.IsComplexType && context.Metadata.ModelType == typeof(string))
{
//简单类型
var loggerFactory = (ILoggerFactory)context.Services.GetService(typeof(ILoggerFactory));
return new SkySimpleTypeModelBinder(new SimpleTypeModelBinder(context.Metadata.ModelType, loggerFactory));
}
if (context.Metadata.IsComplexType && !context.Metadata.IsCollectionType)
{
//复杂类型
var propertyBinders = context.Metadata.Properties
.ToDictionary(modelProperty => modelProperty, modelProperty => context.CreateBinder(modelProperty));
var loggerFactory = (ILoggerFactory)context.Services.GetService(typeof(ILoggerFactory));
return new SkyComplexTypeModelBinder(propertyBinders, loggerFactory);
}
return null;
}
}

注册服务

    public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
}); //注册自定义的模型绑定
services.AddControllersWithViews(
options => options.ModelBinderProviders.Insert(, new MyModelBinderProvider())
).AddNewtonsoftJson();
services.AddRazorPages();
}

谢谢浏览!

最新文章

  1. C#如何创建泛型类T的实例
  2. CentOS 5.8/6.7若干优化
  3. uva 242
  4. 七. CSS字体
  5. elasticsearch 基础语句
  6. 吴恩达深度学习笔记1-神经网络的编程基础(Basics of Neural Network programming)
  7. 手动设定统计数据 set_table_stats
  8. Chap3:区块链的衍生技术[《区块链中文词典》维京&amp;甲子]
  9. openresty + lua 2、openresty 连接 redis,实现 crud
  10. Windows 网卡超过序列
  11. hdu 4112 Break the Chocolate 贪心
  12. arcgis server 无法手动删除切片
  13. 毕业设计总结(1)-canvas画图
  14. 监听器(Listener)学习(二)在开发中的常见应用
  15. 转!java产生不重复随机数
  16. C# 6新特性简单总结
  17. 201621123033 《Java程序设计》第10周学习总结
  18. Unicode字符集和多字节字符集关系
  19. 不可见类有抽象父类,spring配置子类bean,注入父类,aop就可以切父类的方法
  20. web结对项目

热门文章

  1. 基于C#反射机制的工厂模式
  2. SpringBoot JPA 专题
  3. BZOJ 1090 - 区间dp
  4. C#连接Sqlserver代码
  5. HTML_ul无序列表
  6. matlab 格式化文本文件的解析
  7. Mac的GUI哲学
  8. 转载:Adb远程连接Android系统(通过网络使用ADB(Connect to android with wifi))
  9. c语言学习笔记(13)——链表
  10. go语言刷leetcode - 14 Longest Common Prefix