MVC06

1.校验机制

我们可以在Model中使用属性进行校验

using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;

namespace MvcMovie.Models
{
    public class Movie
    {
        public int ID { get; set; }
        [StringLength(60,MinimumLength = 1)]
        [Required]
        public string Title { get; set; }

        // 添加命名空间
        // Display属性,修改属性名称
        [Display(Name ="Release Date")]
        // 该属性值类型限定
        [DataType(DataType.Date)]
        // 限定格式
        [DisplayFormat(DataFormatString ="{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)]
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        [Range(1,1000)]
        [DataType(DataType.Currency)]
        public decimal Price { get; set; }
    }

    public class MovieDBContext : DbContext
    {
        public DbSet<Movie> Movies { get; set; }
    }
}

坑:https://blog.csdn.net/yzamo/article/details/38901141

这里做的校验是客户端进行的校验,校验失败的数据不会被提交到服务器

 

2.服务器端校验

当浏览器禁用js无法在客户端进行校验时,需要进行服务端的校验。

当我们主动禁用浏览器js脚本执行后,也能够进行服务器端的校验

 

3.C#处理异常

使用try-catch语句处理异常

int x = 0;
try{
int y = 1/0;
}
catch(NullReferenceException e)
{
  Console.WriteLine(e.Message);
  throw;
}
catch(DivideByZeroException e)
{
  Console.WriteLine(e.Message);
  throw;
}
finally { Console.log("you will see this msg finally") }

catch的错误类型从上到下依次匹配。

模糊匹配请使用Exception

Exception消耗较大,比较占用资源

posted on 2020-01-30 21:53  Tanqurey  阅读(154)  评论(0编辑  收藏  举报