问题总汇

1.解决.net网站第一次访问慢的问题

答:让IIS自己运行一次,设置方法:IIS manager -> your application pool -> Advanced Settings... -> change "Start Mode" to "AlwaysRunning"

IIS管理器- >应用程序池- >高级设置…- >“启动模式”改为“AlwaysRunning”


2.模型验证

if (ModelState.IsValid){}//直接验证POST接收模型


1)DataAnnotations验证+DataAnnotationsExtensions(DataAnnotations扩展库)

适应于MVC、webapi项目

DataAnnotationsExtensions  nuget: Install-Package DataAnnotationsExtensions


执行验证方法

ValidateModel(model)

TryValidateModel(model)

应用方式


注:扩展类要跟数据库表生成类在一个命名空间

 非MVC项目模型验证(但依赖于MVC)

//模型验证
    public class FakeControllerValidator : System.Web.Mvc.Controller
    {
        public FakeControllerValidator()
        {
            this.ControllerContext = new System.Web.Mvc.ControllerContext(new System.Web.Routing.RequestContext(new HttpContextWrapper(System.Web.HttpContext.Current), new System.Web.Routing.RouteData()), this);
        }
        public bool Validate(object model, out System.Web.Mvc.ModelStateDictionary modelStateDictionary)
        {
            bool isValid = TryValidateModel(model);
            modelStateDictionary = ModelState;
            return isValid;
        }

        public bool Validate(object model)
        {
            bool isValid = TryValidateModel(model);
            return isValid;
        }
    }

2) FluentValidation 模型验证

适应于winform  webform  mvc等

using FluentValidation;
using FluentValidation.Results;

class Program
    {
        static void Main(string[] args)
        {
            Class1 c = new Class1();
            c.name = "我的名字*";
            c.id = 2;
            CustomerValidator validator = new CustomerValidator();
            ValidationResult results = validator.Validate(c);
            bool validationSucceeded = results.IsValid;
        }
    }

    public class CustomerValidator : AbstractValidator<Class1>
    {
        public CustomerValidator()
        {
            //长度为5-7位
            RuleFor(class1 => class1.name).NotNull().Length(5, 7);
            //ID大于0
            RuleFor(class1 => class1.id).GreaterThan(0);
            //ID在1-3之间
            RuleFor(class1 => class1.id).InclusiveBetween(1, 3);
        }
    }

nuget: Install-Package FluentValidation -Pre

参考网址:http://www.cnblogs.com/asxinyu/p/dotnet_Opensource_project_FluentValidation_1.html

3.事物

//创建事务
using (TransactionScope scope = new TransactionScope())
{
//=======
//主体
//=======


//执行事务
scope.Complete();
}


4.创建非null和空''索引 sql server

CREATE UNIQUE NONCLUSTERED INDEX 索引名
ON 表名(字段名)
WHERE 字段名 is not null and 字段名 != ''
GO




阅读更多

没有更多推荐了,返回首页