翼度科技»论坛 编程开发 .net 查看内容

第四单元 视图与模型

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
  1. create database MvcUnit4;
  2. go
  3. use MvcUnit4;
  4. go
  5. create table Product
  6. (
  7.     Id bigint primary key,
  8.     ProductName varchar(30),
  9.     CategoryName varchar(30),
  10.     Price decimal(10,2),
  11.     Remark varchar(200),
  12.     CreatedUserId bigint,
  13.     UpdatedUserId bigint,
  14.     CreatedTime datetime,
  15.     UpdatedTime datetime,
  16.     Deleted bit
  17. );
  18. insert into Product values(1,'C# 入门','.Net 开发',25,'',1,1,getdate(),getdate(),0);
  19. insert into Product values(2,'Sql基础','数据库',25,'',1,1,getdate(),getdate(),0);
  20. insert into Product values(3,'SQL高级','数据库',120,'',1,1,getdate(),getdate(),0);
  21. insert into Product values(4,'Ado.Net','数据库访问技术',25,'',1,1,getdate(),getdate(),0);
  22. insert into Product values(5,'EntityFramework','数据库访问技术',120,'',1,1,getdate(),getdate(),0);
  23. insert into Product values(6,'C#高级','.Net 开发',140,'',1,1,getdate(),getdate(),0);
  24. insert into Product values(7,'Asp.net Mvc Core','.Net 开发',25,'',2,2,getdate(),getdate(),0);
  25. insert into Product values(8,'MySql基础','数据库',25,'',2,2,getdate(),getdate(),0);
  26. go
  27. create table UserInfo
  28. (
  29.     Id bigint primary key,
  30.     UserName varchar(30),
  31.     NickName varchar(30),
  32.     Pwd varchar(50),
  33.     CreatedUserId bigint,
  34.     UpdatedUserId bigint,
  35.     CreatedTime datetime,
  36.     UpdatedTime datetime,
  37.     Deleted bit
  38. );
  39. insert into UserInfo values(1,'renwoxing','任我行','123456',0,0,getdate(),getdate(),0);
  40. insert into UserInfo values(2,'xiaoming','小明','123456',0,0,getdate(),getdate(),0);
复制代码
 
1. 视图模型-ViewModel

1. 为什么要建立视图模型

不同的业务场景,我们最好是建立不同的ViewModel来与与之对应。我们弄个实际开发的例子来进行说明。
  1. // 父类数据实体
  2. public class BaseEntity
  3. {
  4.     public long Id{ get;set;}
  5.     public long CreatedUserId { get; set;}
  6.     public long UpdatedUserId { get; set;}
  7.     public DateTime CreatedTime { get; set; }= DateTime.Now;
  8.     public DateTime UpdatedTime { get; set; }= DateTime.Now;
  9.     public bool Deleted { get; set; } = false;
  10. }
  11. // 商品数据实体
  12. class Product : BaseEntity {
  13.     public String ProductName {get; set;}
  14.     public decimal Price { get; set; }
  15.     public String CategoryName { get; set; }
  16.     public String Remark { get; set; }
  17. }
  18. // 用户数据实体
  19. class UserInfo : BaseEntity{
  20.     public String UserName { get;set;}
  21.     public String NickName { get;set;}
  22.     public String Pwd { get;set;}
  23. }
复制代码
 
此时前端需要展示的数据项如下:
商品名,价格,分类,添加人,维护人,最后维护时间
此时你的商品视图模型应该是:
  1. class ProductViewModel{
  2.     public long Id;
  3.     public String ProductName {get; set;}
  4.     public decimal Price { get; set; }
  5.     public String CategoryName { get; set; }
  6.     public String CreatedNickName { get; set; }
  7.     public String UpdatedNickName { get; set; }
  8.     public DateTime UpdatedTime { get; set; }
  9. }
复制代码
 
使用视图模型的好处:
面对那些业务场景不需要的字段我们不应该返回给前端,

  • 方便此业务场景的维护,就算将来当前业务场景发生变化,也不至于影响到其他同学的调用。
  • 字段太多,对于其他调用者来说不太友好,别人不知道这个字段是干嘛用的,特别是现在流行微服务开发,当我们给别人提供接口时,切记要记得“按需所给” ,否则别人可能会为了你这些“没用的字段” 而 去大费周章的去东挪西凑。
  • 更符合当前的DDD开发模式。
 
添加或者修改的视图:
  1. // 父类输入数据
  2. class BaseInput{
  3.     public long Id{ get;set;}
  4.     public long CreatedUserId { get; set;}
  5.     public long UpdatedUserId { get; set;}
  6.     public DateTime CreatedTime => DateTime.Now;
  7.     public DateTime UpdatedTime => DateTime.Now;
  8.     public bool Deleted => false;
  9. }
  10. class ProductInput : BaseInput{
  11.     public String ProductName {get; set;}
  12.     public decimal Price { get; set; }
  13.     public String CategoryName { get; set; }
  14.     public String Remark {get;set;}
  15. }
复制代码
 
BaseInput 与 BaseEntity 虽然 字段一样,但是所处的业务领域不一样。比如我们可能会在BaseEntity 类中对字段Id 标识 [Key] 特性来说明它是主键字段,而BaseInput 类中的Id 则只是作为一个输入参数而已,换句话说,此Id而非彼Id。
ProductInput 类 虽然 与 Product 类 字段一样,但这只是个例,事实在微服务开发过程中,很多时候方法的入参字段列表与数据实体的字段列表是不一样的。目前大家只需要掌握这种思想即可,随着大家的开发经验的积累,会慢慢的体会到这种模式的好处。
2. 属性映射

虽然建立视图模型给我们带来了业务领域驱动的好处,但是同样也给我们带来了一些编码上的麻烦,代码如下:
  1. public class ProductController:Controller
  2. {
  3.     private readonly IdWorker _idWorker;
  4.     private readonly IProductService _productService;
  5.     public ProductController(IProductService productService)
  6.     {
  7.         _productService = productService;
  8.         _idWorker = SnowflakeUtil.CreateIdWorker();
  9.     }
  10.     public IActionResult Create(ProductInput input)
  11.     {
  12.         // 将ProductInput 转换为 Product 数据实体对象
  13.          // (实际开发场景下,Input实体转换为数据实体过程应放至业务逻辑层)
  14.         Product entity = new()
  15.         {
  16.             Id = _idWorker.NextId(), // 雪花Id
  17.             CategoryName = input.CategoryName,
  18.             Price = input.Price,
  19.             ProductName = input.ProductName
  20.         };
  21.         _productService.Save(entity);
  22.         return Ok("添加成功");
  23.     }
  24. }
复制代码
 
如果我们每一个业务对象都需要这样手动的为每个属性赋值的话,那对于我们程序员来说简直就是把技术活变成了体力活了。接下来我们需要介绍另外一款组件:AutoMap自动映射
 
2. AutoMap 组件-自动映射

需要安装的包:

  • AutoMapper.Extensions.Microsoft.DependencyInjection
快速开始

首先,配置映射关系
  1. public class OrganizationProfile : Profile
  2. {
  3.     public OrganizationProfile()
  4.     {
  5.         CreateMap<Foo, FooDto>();
  6.     }
  7. }
复制代码
 
如果是控制台应用,则:
  1. var configuration = new MapperConfiguration(cfg => {
  2.     //cfg.CreateMap<Foo, Bar>();
  3.     cfg.AddProfile<OrganizationProfile>();//或者cfg.AddProfile(new OrganizationProfile());
  4. });
  5. var mapper=configuration.CreateMapper();//或者var mapper=new Mapper(configuration);
  6. var dest=mapper.Map<OrderDto>(order);
复制代码
 
如果ASP.NET Core应用,则(需安装AutoMapper.Extensions.Microsoft.DependencyInjection)
  1. //1. 配置服务里Add,入参类型为params
  2. build.Services.AddAutoMapper(typeof(OrganizationProfile));
  3. //2. 然后在Controller里使用即可:
  4. public XXXController(IMapper mapper)
  5. {
  6.     _mapper = mapper;
  7.     var dest=mapper.Map<OrderDto>(order);
  8. }
复制代码
 
1.常见配置

1.1 Profile的配置

除了上述的手动添加profile,还可以自动扫描profile并添加:
  1. //方法1
  2. var configuration = new MapperConfiguration(cfg => cfg.AddMaps(myAssembly));
  3. //方法2:通过程序集名
  4. var configuration = new MapperConfiguration(cfg =>
  5.     cfg.AddMaps(new [] {
  6.         "Foo.UI", // 程序集名称
  7.         "Foo.Core"
  8.     });
  9. );
复制代码
 
1.2 命名方式 camelCase/PascalCase

作用:驼峰命名与Pascal命名的兼容。
以下全局配置会映射property_name到PropertyName
  1. builder.Services.AddAutoMapper(cfg =>
  2. {
  3.     cfg.AddProfile<CustomerProfile>();
  4.     // propertyName ------> PropertyName 默认是支持的
  5.     cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
  6.     cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
  7. });
复制代码
 
或者针对某个profile进行配置(这种方式全局通用):
  1. public class OrganizationProfile : Profile
  2. {
  3.     public OrganizationProfile()
  4.     {
  5.         SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
  6.         DestinationMemberNamingConvention = new PascalCaseNamingConvention();
  7.         //Put your CreateMap... Etc.. here
  8.     }
  9. }
复制代码
 
1.3 映射时针对某些字符进行替换
  1. var configuration = new MapperConfiguration(c =>
  2. {
  3.     c.ReplaceMemberName("Ä", "A");
  4.     c.ReplaceMemberName("í", "i");
  5.     c.ReplaceMemberName("Airlina", "Airline");
  6. });
复制代码
 
进行以上配置之后,会自动将Äbc映射到Abc上,将íng映射到ing上,将AirlinaMark映射到AirlineMark上。
1.4 映射时匹配前缀或后缀
  1. var configuration = new MapperConfiguration(cfg => {
  2.     cfg.RecognizePrefixes("frm");
  3.     //cfg.RecongizePostfixes("后缀");
  4.     cfg.CreateMap<Source, Dest>();
  5. });
复制代码
 
这样frmValue就可以map到Value上。
Automapper默认匹配了Get前缀,如果不需要可以清除:
  1. cfg.ClearPrefixes();//清除所有前缀
复制代码
 
 
★1.5控制哪些属性和字段能够被映射

使用ShouldMapField和ShouldMapProperty
  1. cfg.ShouldMapField = fi => false;
  2. cfg.ShouldMapProperty = pi =>pi.GetMethod != null && (pi.GetMethod.IsPublic || pi.GetMethod.IsPrivate);
复制代码
 
默认所有public的field和property都会被map,也会map private 的setter,但是不会map整个property都是internal/private的属性。
1.6 提前编译

默认是调用的时候才编译映射,但是可以要求AutoMapper提前编译,但是可能会花费点时间:
  1. var configuration = new MapperConfiguration(cfg => {});
  2. configuration.CompileMappings();
复制代码
 
 
2. Projection(映射)

AutoMapper只会映射扁平的类,嵌套的类,继承的类,需要进行手动配置。成员名称不一致时,也要手动配置映射。
AutoMapper默认会自动映射以下类型,并且映射时会先清空dest对应成员的数据:

  • IEnumerable
  • IEnumerable
  • ICollection
  • ICollection
  • IList
  • IList
  • List
  • Arrays
这几个集合之间可以相互映射,如:
  1. mapper.Map<Source[], IEnumerable<Destination>>(sources);
复制代码
 
★2.1 手动控制某些成员的映射

ForMember+MapFrom
  1. // Configure AutoMapper
  2. var configuration = new MapperConfiguration(cfg =>
  3.   cfg.CreateMap<CalendarEvent, CalendarEventForm>()
  4.     .ForMember(dest => dest.EventDate, opt => opt.MapFrom(src => src.Date.Date))
  5.     .ForMember(dest => dest.EventHour, opt => opt.MapFrom(src => src.Date.Hour))
  6.     .ForMember(dest => dest.EventMinute, opt => opt.MapFrom(src => src.Date.Minute))
  7. );
复制代码
 
将src的Date.Date映射到dest的EventDate成员上
★2.2 嵌套(Nested)类和继承类映射

某些成员可能是一个类,那么这个类也要配置映射。同理一个类的父类也要配置映射。
  1. var config = new MapperConfiguration(cfg => {
  2.     cfg.CreateMap<OuterSource, OuterDest>();
  3.     cfg.CreateMap<InnerSource, InnerDest>();
  4. });
复制代码
 
★2.3 映射继承与多态 Include/IncludeBase

假如ChildSource继承ParentSource,ChildDestination继承ParentDestination。并且有这么一个业务,ParentSource src=new ChildSource()需要把src转为ParentDestination。
直接转的话肯定会有member丢失,所以要进行如下配置:
  1. var configuration = new MapperConfiguration(c=> {
  2.     c.CreateMap<ParentSource, ParentDestination>()
  3.          .Include<ChildSource, ChildDestination>();
  4.     c.CreateMap<ChildSource, ChildDestination>();
  5. });
复制代码
 
或者也可以这么写:
  1. CreateMap<ParentSource,ParentDestination>();
  2. CreateMap<ChildSource,ChildDestination>()
  3.     .IncludeBase<ParentSource,ParentDestination>();
复制代码
 
如果有几十个类都继承了ParentSource和ParentDestination,那么上述两种方法就太啰嗦了,可以这么写:
  1. CreateMap<ParentSource,ParentDestination>().IncludeAllDerived();
  2. CreaetMap<ChildSource,ChildDestination>();
复制代码
 
更复杂的用法参考:Mapping-inheritance
2.4 构造函数映射

如果dest构造函数的入参名和src的某个member一致,则不用手动配置,automapper会自动支持:
  1. public class Source{
  2.     public Source(int value) {
  3.         this.Value = value;
  4.     }
  5.    public int Value { get; set; }  
  6. }
  7. public class SourceDto {
  8.     public SourceDto(int value) {
  9.         _value = value;
  10.     }
  11.     private int _value;
  12.     public int Value {
  13.         get { return _value; }
  14.     }
  15. }
复制代码
 
如果这里构造的入参不叫value而叫value2,则要进行如下配置:
  1. var configuration = new MapperConfiguration(cfg =>
  2.   cfg.CreateMap<Source, SourceDto>()
  3.     .ForCtorParam("value2", opt => opt.MapFrom(src => src.Value))
  4. );
复制代码
 
也可以禁用构造函数映射:
  1. var configuration = new MapperConfiguration(cfg => cfg.DisableConstructorMapping());
复制代码
 
也可以配置什么情况下不用构造函数映射:
  1. var configuration = new MapperConfiguration(cfg => cfg.ShouldUseConstructor = ci => !ci.IsPrivate);//不匹配私有构造函数
复制代码
 
 
2.5 复杂类映射成扁平类
  1. public class Src
  2. {
  3.     public Customer Customer {get;set;}
  4.     public int GetTotal()
  5.     {
  6.         return 0;
  7.     }
  8. }
  9. public class Customer
  10. {
  11.     public string Name {get;set;}
  12. }
  13. public class Dest
  14. {
  15.     public string CustomerName {get;set;}
  16.     public int Total {get;set;}
  17. }
复制代码
 
则src可以自动映射成dest,包括CustomerName和Total字段。这种与手动配置
  1. cfg.CreateMap<Src,Dest>().ForMember(d=>d.CustomerName,opt=>opt.MapFrom(src=>src.Customer.Name))
复制代码
 
然后进行映射的方式类似。
映射时AutoMapper发现,src里没有CustomerName这个成员,则会将dest的CustomerName按照PascalCase的命名方式拆分为独立的单词。所以CustomerName会映射到src的Customer.Name。如果想禁用这种自动映射,则调用cfg.DestinationMemberNamingConvention = new ExactMatchNamingConvention();使用精确映射。
如果感觉AutoMapper的这种基于PascalCase命名拆分的自动映射没法满足你的需要,则还可以手动指定某些成员的映射:
  1. class Source
  2. {
  3.     public string Name { get; set; }
  4.     public InnerSource InnerSource { get; set; }
  5.     public OtherInnerSource OtherInnerSource { get; set; }
  6. }
  7. class InnerSource
  8. {
  9.     public string Name { get; set; }
  10.     public string Description { get; set; }
  11. }
  12. class OtherInnerSource
  13. {
  14.     public string Name { get; set; }
  15.     public string Description { get; set; }
  16.     public string Title { get; set; }
  17. }
  18. class Destination
  19. {
  20.     public string Name { get; set; }
  21.     public string Description { get; set; }
  22.     public string Title { get; set; }
  23. }
  24. cfg.CreateMap<Source, Destination>().IncludeMembers(s=>s.InnerSource, s=>s.OtherInnerSource);
  25. cfg.CreateMap<InnerSource, Destination>(MemberList.None);
  26. cfg.CreateMap<OtherInnerSource, Destination>();
  27. var source = new Source { Name = "name", InnerSource = new InnerSource{ Description = "description" }, OtherInnerSource = new OtherInnerSource{ Title = "title",Description="descpripiton2" } };
  28. var destination = mapper.Map<Destination>(source);
  29. destination.Name.ShouldBe("name");
  30. destination.Description.ShouldBe("description");
  31. destination.Title.ShouldBe("title");
复制代码
 
IncludeMembers参数的顺序很重要,这也就是dest的Description为“description”而不是“description2”的原因,因为InnerSource的Description属性最先匹配到了Destination的Description属性。
IncludeMembers相当于把子类打平添加到了src里,并进行映射。
★2.6 映射反转(Reverse Mapping)

reverse mapping一般在CreateMap方法或者ForMember等方法之后,相当于src和dest根据你自己的配置可以相互映射,少写一行代码:
  1. cfg.CreateMap<Order, OrderDto>().ReverseMap();
  2. //等同于以下两句
  3. cfg.CreateMap<Order,OrderDto>();
  4. cfg.CreateMap<OrderDto,Order>();
复制代码
 
如果还想对reverse map进行自定义(大多数情况下都不需要),则可以使用ForPath:
  1. cfg.CreateMap<Order, OrderDto>()
  2.   .ForMember(d => d.CustomerName, opt => opt.MapFrom(src => src.Customer.Name))
  3.   .ReverseMap()
  4.   .ForPath(s => s.Customer.Name, opt => opt.MapFrom(src => src.CustomerName));
复制代码
 
注意:
如果reverse之前定义了一些诸如ForMember之类的约束,这些约束是不会自动reverse的,需要手动配置。以下代码配置了不管从Order映射到OrderDto还是从OrderDto映射到Order,都忽略CustomerName属性。
  1. cfg.CreateMap<Order, OrderDto>()
  2.     .ForMember(d => d.CustomerName, opt => opt.Ignore())
  3.     .ReverseMap()
  4.     .ForMember(d => d.CustomerName, opt => opt.Ignore())
复制代码
 
 
2.7 使用特性映射

(C#称作特性,Java叫注解)
  1. [AutoMap(typeof(Order))]
  2. public class OrderDto {}
复制代码
 
等同于CreateMap()。然后配置的时候用AddMaps方法:
  1. var configuration = new MapperConfiguration(cfg => cfg.AddMaps("MyAssembly"));
  2. var mapper = new Mapper(configuration);
复制代码
 
特性里还有如下参数供设置:
  1. ReverseMap (bool)
  2. ConstructUsingServiceLocator (bool)
  3. MaxDepth (int)
  4. PreserveReferences (bool)
  5. DisableCtorValidation (bool)
  6. IncludeAllDerived (bool)
  7. TypeConverter (Type)
复制代码
 
TypeConverter (Type)映射注解的更多信息参考:Attribute-mapping
2.8 动态类型Dynamic到普通类型的映射

默认就支持,不用手动CreateMap
★2.9 泛型映射
  1. public class Source<T> {}
  2. public class Destination<T> {}
  3. var configuration = new MapperConfiguration(cfg => cfg.CreateMap(typeof(Source<>), typeof(Destination<>)));
复制代码
 
注意:CreateMap不需要传具体的T
 
★3. 扩展IQueryable(与EF等ORM配合使用)

需要安装nuget包:AutoMapper.EF6
这个功能存在的意义是为了解决一些orm框架返回的是IQueryable类型,使用一般的mapper.Map做转换时,会查询出来整行数据,然后再挑选出来某些字段做映射,会降低性能的问题。解决方法是使用ProjectTo:
  1. public class OrderLine
  2. {
  3.     public int Id { get; set; }
  4.     public int OrderId { get; set; }
  5.     public Item Item { get; set; }
  6.     public decimal Quantity { get; set; }
  7. }
  8. public class Item
  9. {
  10.     public int Id { get; set; }
  11.     public string Name { get; set; }
  12. }
  13. public class OrderLineDTO
  14. {
  15.     public int Id { get; set; }
  16.     public int OrderId { get; set; }
  17.     public string Item { get; set; }
  18.     public decimal Quantity { get; set; }
  19. }
  20. var configuration = new MapperConfiguration(cfg =>
  21.                                             cfg.CreateMap<OrderLine, OrderLineDTO>()
  22.                                             .ForMember(dto => dto.Item, conf => conf.MapFrom(ol => ol.Item.Name)));
  23. public List<OrderLineDTO> GetLinesForOrder(int orderId)
  24. {
  25.     using (var context = new orderEntities())
  26.     {
  27.         return context.OrderLines.Where(ol => ol.OrderId == orderId)
  28.             .ProjectTo<OrderLineDTO>(configuration).ToList();
  29.     }
  30. }
  31. //这样查Item表的时候,只会select name字段。
复制代码
 
3.1 枚举映射

大多数情况下都不需要进行此项的配置。考虑以下两个枚举:
  1. public enum Source
  2. {
  3.     Default = 0,
  4.     First = 1,
  5.     Second = 2
  6. }
  7. public enum Destination
  8. {
  9.     Default = 0,
  10.     Second = 2
  11. }
复制代码
 
如果想把Source.First映射到Destination.Default上,则需要安装AutoMapper.Extensions.EnumMapping nuget包,然后进行如下配置:
  1. internal class YourProfile : Profile
  2. {
  3.     public YourProfile()
  4.     {
  5.         CreateMap<Source, Destination>()
  6.             .ConvertUsingEnumMapping(opt => opt
  7.                 // optional: .MapByValue() or MapByName(), without configuration MapByValue is used
  8.                 .MapValue(Source.First, Destination.Default)
  9.             )
  10.             .ReverseMap();
  11.     }
  12. }
复制代码
 
默认情况下AutoMapper.Extensions.EnumMapping 会将源枚举里所有的数据根据枚举值或枚举名称映射到目标枚举上。如果找不到且启用了EnumMappingValidation,则会抛出异常。
4.进阶

★4.1 类型转换(可针对所有的maps生效)

当从可空类型与不可空类型相互转换时,当string与int等转换时,就需要自定义类型转换。一般有以下三种方法:
  1. void ConvertUsing(Func<TSource, TDestination> mappingFunction);
  2. void ConvertUsing(ITypeConverter<TSource, TDestination> converter);
  3. void ConvertUsing<TTypeConverter>() where TTypeConverter : ITypeConverter<TSource, TDestination>;
复制代码
 
简单情景下使用第一种,复杂情景下自定义类去实现ITypeConverter接口。综合举例如下:
  1. public class Source
  2. {
  3.     public string Value1 { get; set; }
  4.     public string Value2 { get; set; }
  5.     public string Value3 { get; set; }
  6. }
  7. public class Destination
  8. {
  9.     public int Value1 { get; set; }
  10.     public DateTime Value2 { get; set; }
  11.     public Type Value3 { get; set; }
  12. }
  13. public void Example()
  14. {
  15.     var configuration = new MapperConfiguration(cfg => {
  16.         cfg.CreateMap<string, int>().ConvertUsing(s => Convert.ToInt32(s));
  17.         cfg.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());
  18.         cfg.CreateMap<string, Type>().ConvertUsing<TypeTypeConverter>();
  19.         cfg.CreateMap<Source, Destination>();
  20.     });
  21.     configuration.AssertConfigurationIsValid();
  22.     var source = new Source
  23.     {
  24.         Value1 = "5",
  25.         Value2 = "01/01/2000",
  26.         Value3 = "AutoMapperSamples.GlobalTypeConverters.GlobalTypeConverters+Destination"
  27.     };
  28.     Destination result = mapper.Map<Source, Destination>(source);
  29.     result.Value3.ShouldEqual(typeof(Destination));
  30. }
  31. public class DateTimeTypeConverter : ITypeConverter<string, DateTime>
  32. {
  33.     public DateTime Convert(string source, DateTime destination, ResolutionContext context)
  34.     {
  35.         return System.Convert.ToDateTime(source);
  36.     }
  37. }
  38. public class TypeTypeConverter : ITypeConverter<string, Type>
  39. {
  40.     public Type Convert(string source, Type destination, ResolutionContext context)
  41.     {
  42.         return Assembly.GetExecutingAssembly().GetType(source);
  43.     }
  44. }
复制代码
 
4.2 通过MapFrom自定义值解析(Value Resolve )

针对的是某一个map的某一个member,有3种写法:
  1. MapFrom<TValueResolver>
  2.     MapFrom(typeof(CustomValueResolver))
  3.     MapFrom(new CustomResolver())
  4.     public class Source
  5.     {
  6.         public int Value1 { get; set; }
  7.         public int Value2 { get; set; }
  8.     }
  9. public class Destination
  10. {
  11.     public int Total { get; set; }
  12. }
  13. public class CustomResolver : IValueResolver<Source, Destination, int>
  14. {
  15.     public int Resolve(Source source, Destination destination, int member, ResolutionContext context)
  16.     {
  17.         //可以添加其他逻辑
  18.         return source.Value1 + source.Value2;
  19.     }
  20. }
  21. var configuration = new MapperConfiguration(cfg =>
  22.                                             cfg.CreateMap<Source, Destination>()
  23.                                             .ForMember(dest => dest.Total, opt => opt.MapFrom<CustomResolver>()));
  24. configuration.AssertConfigurationIsValid();
  25. var source = new Source
  26. {
  27.     Value1 = 5,
  28.     Value2 = 7
  29. };
  30. var result = mapper.Map<Source, Destination>(source);
  31. result.Total.ShouldEqual(12);
复制代码
 
这种与一般的MapFrom(src=>src.Value1+src.Value2)区别是可以添加更加复杂的逻辑。
如果想要一个更通用的CustomResolver,不管src和dest是什么类型的都能用,则可以实现IValueResolver接口。但是这个resolver里没法获取dest的value,如果有这种需要的话,可以实现ImemberValueResolver接口,进行更细粒度的控制。
4.3 映射时传入数据

只支持传入键值对格式的数据
  1. cfg.CreateMap<Source, Dest>()
  2.     .ForMember(dest => dest.Foo, opt => opt.MapFrom((src, dest, destMember, context) => context.Items["Foo"]));
  3. mapper.Map<Source, Dest>(src, opt => opt.Items["Foo"] = "Bar");
复制代码
 
 
4.4 条件映射 (Conditions和Preconditions)

符合某些条件时才映射
  1. var configuration = new MapperConfiguration(cfg => {
  2.     cfg.CreateMap<Foo,Bar>()
  3.         .ForMember(dest => dest.baz, opt => opt.Condition(src => (src.baz >= 0)));
  4. });
  5. var configuration = new MapperConfiguration(cfg => {
  6.     cfg.CreateMap<Foo,Bar>()
  7.         .ForMember(dest => dest.baz, opt => {
  8.             opt.PreCondition(src => (src.baz >= 0));
  9.             opt.MapFrom(src => {
  10.                 //有了Precondition,这里的操作有可能不执行,节省资源
  11.             });
  12.         });
  13. });
复制代码
 
Condition()方法会在MapFrom方法后执行,而Preconditions会在MapFrom前执行
4.5 空值替换

作用:如果src为null的话,就给dest一个默认值
  1. var config = new MapperConfiguration(cfg => cfg.CreateMap<Source, Dest>()
  2.                                      .ForMember(destination => destination.Value, opt => opt.NullSubstitute("Other Value")));
  3. var source = new Source { Value = null };
  4. var mapper = config.CreateMapper();
  5. var dest = mapper.Map<Source, Dest>(source);
  6. dest.Value.ShouldEqual("Other Value");
  7. source.Value = "Not null";
  8. dest = mapper.Map<Source, Dest>(source);
  9. dest.Value.ShouldEqual("Not null");
复制代码
 
4.6 ValueTransformers 映射点缀(自己起的名字)

作用:在赋值的时候,我想额外在值上加点东西
  1. var configuration = new MapperConfiguration(cfg => {
  2.     cfg.ValueTransformers.Add<string>(val => val + "!!!");
  3. });
  4. var source = new Source { Value = "Hello" };
  5. var dest = mapper.Map<Dest>(source);
  6. dest.Value.ShouldBe("Hello!!!");
复制代码
 
可以应用到全局、某个Profile、某个Map或某个member。
4.7 映射前或映射后干点额外的事 MapAction

可以提前配置:
  1. var configuration = new MapperConfiguration(cfg => {
  2.     cfg.CreateMap<Source, Dest>()
  3.         .BeforeMap((src, dest) => src.Value = src.Value + 10)
  4.         .AfterMap((src, dest) => dest.Name = "John");
  5. });
复制代码
 
也可以在map时进行配置:
  1. int i = 10;
  2. mapper.Map<Source, Dest>(src, opt => {
  3.     opt.BeforeMap((src, dest) => src.Value = src.Value + i);
  4.     opt.AfterMap((src, dest) => dest.Name = HttpContext.Current.Identity.Name);
  5. });
复制代码
 
MapAction也可以创建全局的。
详细用法:Before-and-after-map-actions
5.AutoMapper的一些默认行为

当映射集合时,如果src的成员为null,则dest的成员会new一个默认值,而不是直接复制为null。如果真要映映射为null,则如下修改:
  1. var configuration = new MapperConfiguration(cfg => {
  2.     cfg.AllowNullCollections = true;
  3.     cfg.CreateMap<Source, Destination>();
  4. });
复制代码
 
这个功能可以配置成全局的、某个profile的或某个member的。
CreateMap,创建映射时前后书写顺序不重要。 映射集合时,会把dest的数据给清除掉,如果不想要这么做,参考github项目AutoMapper.Collection。 假如某个成员的名称为NameAAA,则名为NameAAA的field,与名为NameAAA的property,与名为GetNameAAA的方法,三者之间可以自动相互映射。 类型转换:AutoMapper支持.NET Framework自带的一些基本的类型转换。所以当src的member是非string类型,dest的是string类型时,映射的时候AutoMapper会调用ToString方法。对于那些不支持的类型转换,需要自己定义Type Converter。
 
6.常见问题

6.1 某一个功能用ITypeConverter、IValueConverter、IValueResolver、IMemberValueResover好像都可以实现,我应该用哪个? 这几个的释义如下:
  1. Type converter = Func<TSource, TDestination, TDestination>
  2. Value resolver = Func<TSource, TDestination, TDestinationMember>
  3. Member value resolver = Func<TSource, TDestination, TSourceMember, TDestinationMember>
  4. Value converter = Func<TSourceMember, TDestinationMember>
复制代码
 
这四个的使用方式都是使用ConvertUsing()方法,区别是type converter 是针对全局的,其它三个是针对某个member的。 入参出参也不一样。
6.2 什么时候需要手动调用CreateMap()? 虽然AutoMapper的官方文档,一直都写着在映射之前要先用CreateMap方法进行配置。但在实际使用过程中,我发现并不是任何时候都需要先配置才能用。
假如dest里的每一个member(属性、字段、Get方法)都能在src里找得到,则不需要额外的配置,即下属代码也可以正常运行:
  1. class Person
  2. {
  3.     public string Name { get; set; }
  4.     public int Age { get; set; }  
  5. }
  6. class Person2
  7. {
  8.    public string Name { get; set; }
  9.    public int? Age { get; set; }
  10.    public DateTime BirthTime { get; set; }
  11. }
  12. public class NormalProfile : Profile
  13. {
  14.     public NormalProfile()
  15.     {
  16.        //CreateMap<Person2, Person>();//
  17.     }
  18. }
  19. var cfg = new MapperConfiguration(c =>
  20. {
  21.     c.AddProfile<NormalProfile>();
  22. });
  23. //cfg.AssertConfigurationIsValid();
  24. var mapper = cfg.CreateMapper();
  25. var s3 = mapper.Map<Person>(new Person2 { Name = "Person2" });
复制代码
 
6.3 ReverseMap到底能Reverse哪些东西? 可以Reverse像PascalCase的命名方式拆分的member,就像CreateMap可以自动处理Customer.Name与CustomerName的映射一样。即:CreateMap不需要额外配置正向就能映射的,那 ReverseMap也可以自动反向映射。 opt.MapForm()操作可以被reverse,如CreateMap().ForMember(dest => dest.Name2, opt => opt.MapFrom(src => src.Name)).ReverseMap();,当从Person映射到Person2的时候,Name2也可以直接映射到Name上。 不支持opt.Ignore()反向映射,即CreateMap().ForMember(dest => dest.Name, opt => opt.Ignore()).ReverseMap()支持Person2->Person时,忽略Name属性,但是从Person->Person2时,不会忽略,如果要忽略的话,还需要再加上.ForMember(dest => dest.Name, opt => opt.Ignore())。
6.4 映射时如何跳过某些成员? 使用CreateMap().ForMember(dest=>dest.AAA,opt=>opt.Ignore()),跳过成员AAA的映射。 使用ShouldMapField委托跳过某些字段,使用ShouldMapProperty委托跳过某些属性。
  1. public class NormalProfile : Profile
  2. {
  3.     public NormalProfile()
  4.     {
  5.         ShouldMapField = fi => false;
  6.         CreateMap<Person2, Person>().ReverseMap();
  7.     }
  8. }
  9. 或者
  10. var cfg = new MapperConfiguration(c =>
  11. {
  12.     c.AddProfile<XXXXProfile>();
  13.     c.ShouldMapField = fi => false;
  14. });
复制代码
 
3. Html 辅助标签

1、使用HTML辅助方法输出超链接

1.Html.ActionLink

@Html.ActionLink("链接文字","ActionName") 这是最基本的用法,要跳转的控制器为本视图所在的控制器。链接文字不可以为空字符串、空白字符串或null值,否则会抛出The Value cannot be null or empty的异常
@Html.ActionLink("链接文字","ActionName","ControllerName") 指定链接文字,动作,控制器
@Html.ActionLink("链接文字","ActionName",new{id=123,page=5}) 当需要设定额外的RouteValue时,可以在第三个参数传入object类型的数据
@Html.ActionLink("链接文字","ActionName",null,new{@}) 当需要传入超链接额外的HTML属性时,可以将参数加载到第四个参数上。
@Html.ActionLink("链接文字","ActionName","ControllerName", null,new{@}) 5个参数
 
2.Html.RouteLink

其用法与Html.ActionLink非常相似,但是RouteLink只可以接收路由名称,而不能接收控制器名称和操作名称
@Html.ActionLink("Link Text","AnotherAction")
@Html.RouteLink("Link Text",new {action = "AnotherAction"})
 
2、使用HTML辅助方法输出表单

常用表单输入元素
Html.BeginForm(),输出标签
Html.EndForm(),输出标签
Html.Label(),输出标签
Html.TextBox(),输出标签
Html.TextArea(),输出标签
Html.Password(),输出标签
Html.CheckBox(),输出标签
Html.RadionButton(),输出标签
Html.DropDownList(),输出标签。
Html.ListBox(),输出标签
Html.Hidden(),输出标签
Html.ValidationSummary(),输出表单验证失败时的错误信息摘要
 
1.Html.BeginForm
  1. @using (Html.BeginForm("Search" , "Home" , FormMethod.Get , new{ target = "_blank" })){
  2.     <input type="text" name="q" />
  3.     <input type="submit" value="Search" />
  4. }
复制代码
 
本辅助方法主要是用来产生标签,可以通过using语句来使用。也可以配合Html.EndForm()使用以产生适当的表单结尾
a.使用using语法产生表单标签
b.使用Html.BeginForm辅助方法输出的表单预设输出的method属性是POST,如果想指定为GET的话,可以输入第三个参数
c.每一个HTML辅助方法都包含htmlAttrebutes参数。参数的类型是IDictionary,辅助方法利用字典条目(在对象参数的情形下,就是对象的属性名称和属性值)创建辅助方法生成元素的的特性。
当属性名称遇到C#语言中的保留关键字时,要加一个@符号作为前缀; 带有连字符的C#属性名是无效的,可以用下划线替代。
 
2.Html.Label

 
@Html.Label("Username","账户")
@Html.TextBox("Username")
 
账户

 
专为input元素服务,为其定义标记。for属性规定label与哪个表单元素绑定,label和表单控件绑定方式有两种:
1.将表单控件作为label的内容,这就是隐式绑定,此时不需要for属性,绑定的控件也不需要id属性
2.为标签下的for属性命名一个目标表单的id,这就是显示绑定。
给加了for属性绑定了input控件后,可以提高鼠标用户的用户体验。如果在label元素内点击文本,就会触发此控件。就是说,当用户渲染该标签时,浏览器就会自动将焦点转到和标签相关的表单控件上。
 
3.Html.TextBox 和 Html.TextArea

 
Html.TextBox的重载如下:
@Html.TextBox("Username") id,name的值为Username
@Html.TextBox("Username","will") id,name的值为Username,value的值为will
@Html.TextBox("Username","will",new{ size=32 }) id,name的值为Username,value的值为will,html属性值size=32
 
如果要传递多个html属性值,并且在多处使用,可以按照下面的例子来做
  1. public ActionResult HelperSample1()
  2. {
  3.     IDictionary<string, object> attr = new Dictionary<string, object>();
  4.     attr.Add("size", "32");
  5.     attr.Add("style", "color:red;");
  6.     ViewData["Dictionary"] = attr;
  7.     return View();
  8. }
  9. @{
  10.     var htmlAttribute = ViewData["Dictionary"] as IDictionary<string, object>;
  11. }
  12. @Html.TextBox("name","Value",htmlAttribute)<br />
  13.     @Html.Password("password","Value",htmlAttribute)<br />
  14.     @Html.TextBox("email","Value",htmlAttribute)<br />
  15.     @Html.TextBox("tel","Value",htmlAttribute)<br />
  16.     @Html.Hidden("id","1")
复制代码
 
使用TextArea方法渲染一个能够显示多行文本的元素
@Html.TextArea("text","hello   world")
可以通过指定显示的行数和列数来控制文本区域的大小
@Html.TextArea("text","hello   world",10,80,null) 最后一项为指定的HTML特性
 
4.Html.Hidden 、Html.Password 、Html.RadioButton 、Html.CheckBox

Html.Hidden辅助方法用于渲染隐藏的输入元素 @Html.Hidden("hidetext","1")

 
Html.Password辅助方法用于渲染密码字段,不保留提交值,显示密码掩码 @Html.Password("UserPassword")

密码框的明密文(显示和隐藏) :
  1. $("#id").prop("type","text") $("#id").prop("type","password")
复制代码
 
Html.RadioButton一般组合使用,为用户的单项选择提供一组可选项
@Html.RadioButton("color","red")
@Html.RadioButton("color","blue",true)
@Html.RadioButton("color","green")
 



 
Html.CheckBox辅助方法是唯一一个渲染两个输入元素的辅助方法


 
5.Html.DropDownList 和 Html.ListBox

 
DropDownList和Html.ListBox辅助方法都返回一个元素,DropDownList允许进行单项选择,而ListBox支持多项选择(在要渲染的标记中,把multiple特性的值设置为multiple)
1)不读取数据库的下拉列表
  1. public ActionResult HelperSample2()
  2. {
  3.     List<SelectListItem> listItem = new List<SelectListItem>();
  4.     listItem.Add(new SelectListItem { Text = "是", Value = "1" });
  5.     listItem.Add(new SelectListItem { Text = "否", Value = "0" });
  6.     ViewData["List"] = new SelectList(listItem, "Value", "Text", "");
  7.     return View();
  8. }
复制代码
 
@Html.DropDownList("List",ViewData["List"] as SelectList,"请选择") 参数依次为下拉列表的名字,指定的列表项,默认选择项的值
 
2)数据来自数据库的下拉列表
  1. public ActionResult Index()
  2. {
  3.     var list = new SelectList(db.Students, "Id", "Age", "3"); //参数依次为数据集合,数据值,数据文本,选中项的值
  4.     ViewBag.List = list;
  5.     return View();
  6. }
  7. @Html.DropDownList("List")
复制代码
 
 
6.Html.ValidationSummary

ValidationSummary辅助方法配合数据注解进行验证。(等到讲模型校验时再详讲)
 
3、强类型辅助方法

 
基本上,属于强类型的辅助方法命名方式皆为“原先的名称最后加上For”,使用强类型辅助方法,在View页面的最上方一定要用@model定义出这个View页面的参考数据模型,如果没有生命就无法正常使用强类型辅助方法。
 
Html.LabelFor(),输出标签,显示字段的名字。
Html.TextBoxFor()
Html.TextAreaFor()
Html.PasswordFor()
Html.CheckBoxFor()
Html.RadioButtonFor()
Html.DropDownListFor(),输出标签。
Html.ListBoxFor(),输出标签。
Html.HiddenFor() ,生成HTML窗体的隐藏域。
Html.DisplayNameFor(),显示数据模型在Metadata定义的显示名称。
Html.DisplayTextFor(),显示数据模型的文字资料。
Html.ValidationMessageFor(),显示数据模型当输入验证失败时显示的错误信息。
Html.EditorFor(),用来输出表单域可编辑文本框。
 
4、使用HTML辅助方法载入分部视图

使用Html.Partial载入分布视图
@Html.Partial("Page")
@Html.Partial("Page",Model)
@Html.Partial("Page",ViewData["Model"])
@Html.Partial("Page",Model,ViewData["Model"])
 
RenderPartial辅助方法与Partial非常相似,但RenderPartial不是返回字符串,而是直接写入响应输出流。出于这个原因,必须把RenderPartial放入代码块中,而不能放在代码表达式中,为了说明这一点,下面两行代码向输出流写入相同的内容:
@{Html.RenderPartial("Page");}
@Html.Partial("Page")
一般情况下,因为Partial相对于RenderPartial来说更方便,所以应该选择Partial。然而,RenderPartial拥有较好的性能,因为它是直接写入响应流的,但这种性能优势需要大量的使用才能看出来。
 
使用Html.Action辅助方法,从控制器载入分布视图
Action和RenderAction类似于Partial和RenderPartial辅助方法。Partial辅助方法通常在单独的文件中应用视图标记来帮助视图渲染视图模型的一部分。另一方面,Action执行单独的控制器操作,并显示结果。Action提供了更多的灵活性和重用性,因为控制器操作可以建立不同的模型,可以利用单独的控制器上下文。
  1. public ActionResult GetPartialView()
  2. {
  3.     return PartialView();
  4. }
复制代码
 
利用Controller类型中的PartialView辅助方法来载入分布视图,而这种载入方式与用View辅助方法唯一的差别,仅在于它不会套用母版页面,其它则完全相同。
@Html.Action("GetPartialView");
通过Html.Action与Html.Partial载入分部视图结果是一样的,但载入的过程却差别很大。若使用Html.Partial载入分部视图是通过HtmlHelper直接读取*.cshtml文件,直接执行该视图并取得结果。若使用Html.Action的话,则会通过HtmlHelper对IIS再进行一次处理要求(通过Server.Execute方法),因此,使用Html.Action会重新执行一遍Controller的生命周期
 
 
 
4. 模型校验

无论你编写什么样的网页程序,都需要对用户的数据进行验证,以确数据的有效性和完整性。
ASP.NET MVC3允许你采用一种被称之为“数据注释”的方式来进行数据验证,这种验证包含了客户端浏览器
和服务器端的双重验证。或许你会问为什么要进行两次验证?首先,客户端验证能够直接响应客户,减少了服务
器压力的同时还提高了用户体验,但是你永远不能信任来自客户端的信息(用户可以关闭浏览器的脚本功能,
让你的js验证彻底不起作用),所以服务器端验证也是必须的。如下图所示:
 

 
 
 
1,常规验证(必填字段、字符串长度、正则表达式验证、范围验证)
先看一个添加界面的反馈的错误信息

 
 
图片仅供参考
 
 
模型校验分类
  1. [Display(Name = "账号")]
  2. [Required(ErrorMessage = "请输入账号")]
  3. public String? Account { get; set; }
  4. [Display(Name = "密码")]
  5. [DataType(DataType.Password)]
  6. [Required(ErrorMessage = "请输入密码")]
  7. public String? Password { get; set; }
  8. [Display(Name = "确认密码")]
  9. [DataType(DataType.Password)]
  10. [Compare("Password",ErrorMessage = "两次输入的密码不一致")]
  11. public String? ConfirmPassword { get; set; }
  12. [Display(Name = "爱好")]
  13. public String[]? Hobby { get; set; }
  14. [Display(Name = "性别")]
  15. public bool? Gender { get; set; }
  16. [Display(Name = "祖籍")]
  17. public int Province { get; set; }
  18. [Display(Name = "生日")]
  19. [DataType(DataType.Date)]
  20. [Required(ErrorMessage = "请选择你的生日")]
  21. public DateTime Birthday { get; set; }
  22. [Display(Name = "简介")]
  23. public string? Description { get; set; }
  24. public OrderState OrderState { get; set; }
  25. public enum OrderState{
  26.     WaitPay,
  27.     WaitSend,
  28.     WaitReceive,
  29.     Finish,
  30.     Cancel
  31. }
  32. Required(必填)
  33. [Display(Name = "昵称"),Required(ErrorMessage = "请您输入昵称")]
  34. public string NickName { get; set; }
  35. StringLength(字段串长度)
  36. [Display(Name = "密码"),StringLength(16,MinimumLength = 6,ErrorMessage = "密码长度6-16位")]
  37. public string  Password { get; set; }
复制代码
 
ErrorMessage:需要显示的错误消息,如:密码长度6-16位
maxinumLength:最大长度
MininumLength:最小长度
 

  • RegularExpression正则校验
pattern:正则表达式
ErrorMessage:需要显示的错误消息,如:邮箱格式错误
  1. [Display(Name = "邮箱"),Required(ErrorMessage = "请输入邮箱"),RegularExpression(@"[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+", ErrorMessage = "请输入正确的邮箱地址")]
  2. public string Email { get; set; }
复制代码
 
Mvc 框架提供了专门用于做邮箱校验的类:EmailAddress
 

  • Compare 比较校验
ErrorMessage:需要显示的错误消息,如:两次输入的密码不一致
otherProperty:需要对比的属性,如:Password
  1. [StringLength(16,MinimumLength = 6,ErrorMessage = "密码长度6-16位")]
  2. public string  Password { get; set; }
  3. [Compare(otherProperty = "Password",ErrorMessage="确认密码不一致" )]
  4. public string ConfirmPwd { get; set; }
复制代码
 
 

  • Range 范围约束
  1. [Display(Name = "年龄"),Range(15,35,ErrorMessage = "年龄只能是15-35周岁")]
  2. public int Age { get; set; }
复制代码
 
ErrorMessage:需要显示的错误消息,如:年龄只能为15-35岁 maxinum:最大数值 mininum:最小数值
 
校验错误信息
  1. [HttpPost]
  2. public ActionResult Index(Register reg)
  3. {
  4.     // 因为我们已经做了模型校验了,所以这些判断我们都不需要加了
  5.    
  6.     /*if (String.IsNullOrEmpty(reg.UserName))
  7.     {
  8.         ModelState.AddModelError("UserName", "用户名不能为空");
  9.     }
  10.     else if (reg.UserName.Length < 6)
  11.     {
  12.         ModelState.AddModelError("UserName", "用户名长度不能小于6位");
  13.     }
  14.     if (ModelState.IsValidField("BirthDate") && reg.BirthDate > DateTime.Now)
  15.     {
  16.         ModelState.AddModelError("BirthDate", "生日不能为将来的时间");
  17.     }*/
  18.    
  19.    
  20.     if (ModelState.IsValid) // 只有当所有的表单都校验通过了,IsValid 才为true
  21.     {
  22.         //保存数据
  23.     }
  24.     return View();
  25. }
复制代码
 
ModelState.IsValid :只有当所有的表单都校验通过了,IsValid 才为true
 
上面我们简单的判断了用户名是否为空,长度是否小于6,以及出生日期是否填写的为将来的日期,接着我们还要在Index视图中加入@Html.ValidationSummary(),这样我们才能够看到最后的输出的错误信息,编译然后不输入任何内容点击注册之后将会出现下面的情况:

 
 
 
我们会发现表单压根就提交不了,这是因为客户端验证在工作。获取读者会很奇怪这节只是常规验证,这个是因为出生日期的格式是DateTime是不能为NULL的,而ASP.NET MVC默认情况下就已经为我们做好了。随便输入123到出生日期然后点击注册,页面会反馈下面的错误信息:
 
 

 
 
第一个信息就是我们在控制器中通过if判断语句加进去的,而第二个似乎你会困惑是怎么回事,这是因为模型绑定器中会为我们进行简单的验证,比如日期不能为空,并且日期的格式要正确,这个都是默认的行为。我们可以尝试在用户名中输入123,同时出生日期输入2020/1/1,点击注册,这时候的错误信息都是我们添加的了:

 
 
读者使用过很多系统,错误信息基本上都是显示在对应的输入框的右边,在ASP.NET MVC中一样可以判断,下面我们修改Index视图:
 
  1.     用户名:
  2.     @Html.EditorFor(m => m.UserName)
  3.     @Html.ValidationMessageFor(m => m.UserName)
  4.     出生日期:
  5.     @Html.EditorFor(m => m.BirthDate)
  6.     @Html.ValidationMessageFor(m => m.BirthDate)
复制代码
 
 
这个时候我们在重新提交,错误信息就到右边了。但是笔者还不打算结束掉这节,我如果限制用户名不能为100000怎么办呢?或许读者马上就能写出来,但是这个是模型级的错误,并不是针对这个字段,所以我们在Home控制器的Index方法(响应Post的那个)中继续追加:

 
 
接着修改Index视图:

 
 
特别需要注意:
ModelState.AddModelError() 左边一定要是空键值名,才能在@Html.ValidationSumary() 区域中显示错误
 
然后重新编译,用户名输入100000就可以看到下面的结果:

 
 
这样我们就可以结束这节了。
 
5. 模型绑定
  1. <a target="_blank" href="https://www.cnblogs.com/product/detail/3">详情</a> // 路由传值
  2. <a target="_blank" href="https://www.cnblogs.com/product/detail?id=3">详情</a>
  3. public IActionResult Detail(int id) // id=3
  4. {
  5.    
  6. }
复制代码
 
上述案件就是最简单的一种模型绑定(Model-Binding)。
Model Binder

  • Form 表单值
  • 路由的值
  • QueryString的值
1. Model 自动绑定

视图标签中name属性与Model对象中的属性如果名称一样,则会自动绑定(Model-Binding)。Model-Binding底层原理还是运用反射来实现。
  1. <form asp-action="Submit" asp-controller="Product">
  2.    
  3.         <label for="Id">主键:</label>
  4.         <input class="form-control" name="Id" id="Id"/>
  5.    
  6.    
  7.         <label for="ProductName">商品名称</label>
  8.         <input class="form-control" id="ProductName" name="ProductName"/>
  9.    
  10.    
  11.         <label for="ProductName">商品名称</label>
  12.         <input class="form-control" id="Price" name="Price"/>
  13.    
  14.    
  15.         <label for="ProductCategory">分类名称</label>
  16.         @Html.TextBoxFor(p=>p.ProductCategory.CategoryName,new{@class="form-control"})
  17.    
  18.    
  19.         <input type="submit" class="btn btn-primary" value="提交"/>
  20.    
  21. </form>
  22. public class Category
  23. {
  24.     public int Id { get; set;}
  25.     public String CategoryName { get;set;}
  26. }
  27. public class ProductInput
  28. {
  29.     public int Id { get; set;}
  30.     public String ProductName { get;set;}
  31.     public decimal Price { get;set;}
  32.     public Category ProductCategory { get; set; }
  33. }
  34. public IActionResult Create()
  35. {
  36.     return View();
  37. }
  38. [HttpPost]
  39. public IActionResult Submit(ProductInput input)
  40. {
  41.     return Ok("添加成功");
  42. }
复制代码
 
2. 使用特性绑定

BindAttribute

表示有选择性的绑定

  • 绑定所有属性
    1. [HttpPost]
    2. public IActionResult Submit([Bind] ProductInput input)
    3. {
    4.     return Ok("添加成功");
    5. }
    复制代码
     
    1. 绑定部分属性
    复制代码
  1. [HttpPost]
  2. public IActionResult Submit([Bind("ProductName","Id")] ProductInput input)
  3. {
  4.     return Ok("添加成功");
  5. }
复制代码
 
此时只有ProductName 与 Id属性能绑定成功。
BindNeverAttrubte

作用在Model属性上,表示此属性不应该绑定上。
  1. public class ProductInput
  2. {
  3.     [BindNever] // 表示Id永远不会被绑定上
  4.     public int Id { get; set;}
  5.     public String ProductName { get;set;}
  6.     public String Price { get;set;}
  7.     public Category ProductCategory { get; set; }
  8. }
复制代码
 
BindRequeiredAttribute

作用在Model属性上,与 BindNever 刚好相反,它表示某属性必须要被绑定
  1. public class ProductInput
  2. {
  3.     [BindNever] // 表示Id永远不会被绑定上
  4.     public int Id { get; set;}
  5.     public String ProductName { get;set;}
  6.     [BindRequired]
  7.     public decimal Price { get;set;}
  8.     public Category ProductCategory { get; set; }
  9. }
复制代码
 
3. 指定Binding来源

默认情况下,模型绑定以键值对的形式从 HTTP 请求中的以下源中获取数据:
对于每个目标参数或属性,按照之前列表中指示的顺序扫描源。 有几个例外情况:

  • 路由数据和查询字符串值仅用于简单类型。
  • 上传的文件仅绑定到实现 IFormFile 或 IEnumerable 的目标类型。
如果默认源不正确,请使用下列属性之一来指定源:

  • FromQuery- 从查询字符串中获取值。Request.QueryString
  • FromRoute - 从路由数据中获取值。Request.RouteValues
  • FromForm - 从发布的表单域中获取值。Request.Form
  • FromBody - 从请求正文中获取值。 Request.Body
  • FromHeader - 从 HTTP 标头中获取值。 Rqueset.Headers
特别需要注意:
FromQuery,FromForm,FromBody ,FromHeader ,FromRoute 不要将他们 应用于每个操作方法的多个参数 。如果有多个参数,请将多个参数封装到一个对象中去接收。
FromQuery

指定应使用请求查询字符串绑定参数或属性。

  • Name : 查询字符串字段的名称。
  1. public class ProductQuery
  2. {
  3.     public String Name { get; set;}
  4.     public int StartPrice { get; set;}
  5.     public int EndPrice { get; set;}
  6. }
  7. // 商品查询方法
  8. public IActionResult Search([FromQUery] ProductQuery condition)
  9. {
  10.    
  11. }
复制代码
 
FromBody

一般用于WebApi中。客户端如果是提交json数据时候建议都加上[FromBody]。
如果客户端提交的数据Content-Type如果不为application/json时,会报错,如果要解决报错,需要在接口上加上[FromForm]。
 
将 [FromBody] 特性应用于一个参数,以便从一个 HTTP 请求的正文填充其属性。
将 [FromBody] 应用于复杂类型参数时,应用于其属性的任何绑定源属性都将被忽略。 例如,以下 Create 操作指定从正文填充其 pet 参数:
 
  1. public ActionResult<Pet> Create([FromBody] Pet pet)
复制代码
 
Pet 类指定从查询字符串参数填充其 Breed 属性:
  1. public class Pet
  2. {
  3.     public string Name { get; set; } = null!;
  4.     [FromQuery] // Attribute is ignored.
  5.     public string Breed { get; set; } = null!;
  6. }
复制代码
 
在上面的示例中:

  • [FromQuery] 特性被忽略。
  • Breed 属性未从查询字符串参数进行填充。
输入格式化程序只读取正文,不了解绑定源特性。 如果在正文中找到合适的值,则使用该值填充 Breed 属性。
 
FormForm

指定在请求正文中使用表单数据绑定参数或属性。
  1. [HttpPost]
  2. public IActionResult Submit([FromForm] ProductInput input)
  3. {
  4.     return Ok("添加成功");
  5. }
复制代码
 
 
4. 文件上传-IFormFile

表示使用 HttpRequest 发送的文件。 ASP.NET Core 支持使用缓冲的模型绑定(针对较小文件)和无缓冲的流式传输(针对较大文件)上传一个或多个文件。
属性
ContentDisposition获取上传文件的原始 Content-Disposition 标头。ContentType获取上传文件的原始 Content-Type 标头。FileName从 Content-Disposition 标头中获取文件名。Headers获取上传文件的标头字典。Length获取文件长度(以字节为单位)。Name从 Content-Disposition 标头中获取窗体字段名称。方法
CopyTo(Stream)将上传的文件的内容复制到 target 流中。CopyToAsync(Stream, CancellationToken)异步将上传的文件的内容复制到 target 流。OpenReadStream()打开用于读取上传文件的请求流。上传前需要注意的知识点:

  • 这个name 一定后端接收的参数名(或者属性名)一致
  • enctype 表示表单传输加密类型:
    解析:关于HTML  标签的 enctype 属性  application/x-www-form-urlencoded:在发送前编码所有字符(默认)    multipart/form-data:不对字符编码,或在使用包含文件上传控件的表单时,必须使用该值  text/plain:空格转换为 "+" 加号,但不对特殊字符编码。 enctype:规定了form表单在发送到服务器时候编码方式,有如下的三个值。 1、application/x-www-form-urlencoded。默认的编码方式。但是在用文本的传输和MP3等大型文件的时候,使用这种编码就显得 效率低下。  2、multipart/form-data 。 指定传输数据为二进制类型,比如图片、mp3、文件。  3、text/plain。纯文体的传输。空格转换为 “+” 加号,但不对特殊字符编码
 
安全注意事项

向用户提供向服务器上传文件的功能时,必须格外小心。 攻击者可能会尝试执行以下操作:

  • 执行拒绝服务攻击。
  • 上传病毒或恶意软件。
  • 以其他方式破坏网络和服务器。
降低成功攻击可能性的安全措施如下:

  • 将文件上传到专用文件上传区域,最好是非系统驱动器。 使用专用位置便于对上传的文件实施安全限制。 禁用对文件上传位置的执行权限。†
  • 请勿将上传的文件保存在与应用相同的目录树中。†
  • 使用应用确定的安全的文件名。 请勿使用用户提供的文件名或上传的文件的不受信任的文件名。† 当显示不受信任的文件名时 HTML 会对它进行编码。 例如,记录文件名或在 UI 中显示(Razor 自动对输出进行 HTML 编码)。
  • 按照应用的设计规范,仅允许已批准的文件扩展名。†
  • 验证是否对服务器执行客户端检查。 客户端检查易于规避。
  • 检查已上传文件的大小。 设置一个大小上限以防止上传大型文件。†
  • 文件不应该被具有相同名称的上传文件覆盖时,先在数据库或物理存储上检查文件名,然后再上传文件。
  • 先对上传的内容运行病毒/恶意软件扫描程序,然后再存储文件。
 
FormOptions

BufferBody启用完整请求正文缓冲。 如果多个组件需要读取原始流,请使用此方法。 默认值为 false。BufferBodyLengthLimit如果 BufferBody 已启用,则这是将缓冲的总字节数的限制。 超出此限制的窗体会在分析时引发 InvalidDataException 。 默认为 134,217,728 字节,大约为 128MB。KeyLengthLimit单个键长度的限制。 包含超出此限制的键的窗体将在分析时引发 InvalidDataException 。 默认为 2,048 字节,大约为 2KB。MemoryBufferThreshold如果 BufferBody 已启用,则会在内存中缓冲正文的这多字节。 如果超出此阈值,则缓冲区将改为移动到磁盘上的临时文件。 这也适用于缓冲单个上传文件正文。 默认为 65,536 字节,大约为 64KB。MultipartBodyLengthLimit每个上传文件正文的长度限制。 超出此限制的窗体节将在分析时引发 InvalidDataException 。 默认为 134,217,728 字节,大约为 128MB。MultipartBoundaryLengthLimit边界标识符长度的限制。 具有超出此限制的边界的窗体将在分析时引发 InvalidDataException 。 默认值为 128 字节。MultipartHeadersCountLimit每个上传文件允许的标头数限制。 将合并同名的标头。 超出此限制的窗体节将在分析时引发 InvalidDataException 。 默认值为 16。MultipartHeadersLengthLimit每个上传文件中标头键和值的总长度限制。 超出此限制的窗体节将在分析时引发 InvalidDataException 。 默认为 16,384 字节,大约为 16KB。ValueCountLimit允许的表单条目数的限制。 超出此限制的窗体会在分析时引发 InvalidDataException 。 默认值为 1024。ValueLengthLimit单个窗体值的长度限制。 包含超出此限制的值的窗体将在分析时引发 InvalidDataException 。 默认为 4,194,304 字节,大约为 4MB。 
1. 单文件上传
  1. <form enctype="multipart/form-data" method="post" action="/student/add">
  2.    
  3.         头像:<input type="file" name="HeadPhoto"/>
  4.    
  5.    
  6.     <input type="submit" value="提交"/>
  7. </form>
  8. private readonly IWebHostEnvironment _hostEnvironment;
  9. public StudentController(IWebHostEnvironment hostEnvironment)
  10. {
  11.     _hostEnvironment = hostEnvironment;
  12. }
  13. public IActionResult Add()
  14. {
  15.     return View();
  16. }
  17. [HttpPost]
  18. public IActionResult Add(IFormFile headPhoto)
  19. {
  20.     // 获取文件后缀名
  21.     var extension = Path.GetExtension(headPhoto.FileName);
  22.     // 为文件重命名,防止文件重名
  23.     var fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff")+"."+extension ;
  24.     using FileStream fileStream = new FileStream(
  25.         // 拼接上传路径(upload文件夹必须事先存在)
  26.         Path.Combine(_hostEnvironment.ContentRootPath, "upload", fileName),
  27.         FileMode.Create, FileAccess.Write);
  28.     // 上传
  29.     headPhoto.CopyTo(fileStream);
  30.     return Content("上传成功");
  31. }
复制代码
 
2. 多文件上传
  1. <form enctype="multipart/form-data" method="post" action="/student/add">
  2.    
  3.         头像:<input type="file" name="HeadPhoto" multiple="multiple"/>
  4.    
  5.    
  6.     <input type="submit" value="提交"/>
  7. </form>
  8. [HttpPost]
  9. public IActionResult Add(IFormFile[] headPhoto)
  10. {
  11.     foreach (var formFile in headPhoto)
  12.     {
  13.         // 获取文件后缀名
  14.         var extension = Path.GetExtension(formFile.FileName);
  15.         // 为文件重命名,防止文件重名
  16.         var fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff")+"."+extension ;
  17.         using FileStream fileStream = new FileStream(
  18.             // 拼接上传路径(upload文件夹必须事先存在)
  19.             Path.Combine(_hostEnvironment.ContentRootPath, "upload", fileName),
  20.             FileMode.Create, FileAccess.Write);
  21.         formFile.CopyTo(fileStream);
  22.     }
  23.     return Content("上传成功");
  24. }
复制代码
 
3. 组合其他表单信息
  1. <form enctype="multipart/form-data" method="post" action="/student/add">
  2.    
  3.         姓名:<input asp-for="Name"/>
  4.    
  5.    
  6.         头像:<input type="file" name="HeadPhoto"/>
  7.    
  8.    
  9.     <input type="submit" value="提交"/>
  10. </form>
  11. public class Student
  12. {
  13.     public int Id { get; set; }
  14.     public string? Name { get; set; }
  15.     // 需要上传的文件
  16.     public IFormFile? HeadPhoto { get; set; }
  17. }
  18. // 使用Student对象接收
  19. [HttpPost]
  20. public IActionResult Add(Student student)
  21. {
  22.     // 获取文件后缀名
  23.     var extension = Path.GetExtension(student.HeadPhoto?.FileName);
  24.     // 为文件重命名,防止文件重名
  25.     var fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + extension;
  26.     using FileStream fileStream = new FileStream(
  27.         // 拼接上传路径(upload文件夹必须事先存在)
  28.         Path.Combine(_hostEnvironment.ContentRootPath, "upload", fileName),
  29.         FileMode.Create, FileAccess.Write);
  30.     student.HeadPhoto?.CopyTo(fileStream);
  31.     return Content("上传成功");
  32. }
复制代码
 
4. 大文件上传

将默认最大支持的限制修改为209715200B, 200M。
  1. [RequestFormLimits(MultipartBodyLengthLimit = 209715200)]
  2. [RequestSizeLimit(209715200)]
  3. [HttpPost]
  4. public IActionResult Add(IFormFile headPhoto)
  5. {
  6.     // ...
  7. }
复制代码
 
虽然这种方式可以暂时解决大文件上传,但是这种方式 本质还是使用缓冲的模型绑定,不太推荐使用这种方式来进行大文件上传, 文件上传使用的磁盘和内存取决于并发文件上传的数量和大小。 如果应用尝试缓冲过多上传,站点就会在内存或磁盘空间不足时崩溃。 如果文件上传的大小或频率会消耗应用资源,请使用流式传输。我们推荐使用无缓冲的流式传输
 
无缓冲的流式传输
  1. [HttpPost]
  2. [RequestFormLimits(MultipartBodyLengthLimit = 609715200)]
  3. [RequestSizeLimit(609715200)]
  4. public async Task<IActionResult> UploadLargeFile()
  5. {
  6.     var request = HttpContext.Request;
  7.     if (!request.HasFormContentType ||
  8.         !MediaTypeHeaderValue.TryParse(request.ContentType, out var mediaTypeHeader) ||
  9.         string.IsNullOrEmpty(mediaTypeHeader.Boundary.Value))
  10.     {
  11.         return new UnsupportedMediaTypeResult();
  12.     }
  13.     var reader = new MultipartReader(mediaTypeHeader.Boundary.Value, request.Body);
  14.     var section = await reader.ReadNextSectionAsync();
  15.     while (section != null)
  16.     {
  17.         var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition,
  18.                                                                                  out var contentDisposition);
  19.         if (hasContentDispositionHeader && contentDisposition.DispositionType.Equals("form-data") &&
  20.             !string.IsNullOrEmpty(contentDisposition.FileName.Value))
  21.         {
  22.             // 获取文件后缀名
  23.             var extension = Path.GetExtension(contentDisposition.FileName.Value);
  24.             // 为文件重命名,防止文件重名
  25.             var fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + extension;
  26.             var saveToPath = Path.Combine(_hostEnvironment.ContentRootPath, "upload", fileName);
  27.             using var targetStream = System.IO.File.Create(saveToPath);
  28.             await section.Body.CopyToAsync(targetStream);
  29.             
  30.         }
  31.         section = await reader.ReadNextSectionAsync();
  32.     }
  33.     return Ok("上传成功");
  34. }
复制代码
 
CopyToAsync() 默认以80KB(81920)/次的速度复制至targetStream目标流中,所以大家放心使用,并不是一下子将流量全部占满。
  配套视频链接:

来源:https://www.cnblogs.com/xuyubing/archive/2023/12/06/17879893.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具