ASP.NET MVC(4)

1.MVC验证,MVC3自带的验证有

.NET 框架中的System.ComponentModel.DataAnnotations命名空间包括了众多可为你所用的内置验证特性,介绍用的最多的其中的四个:
[Required], [StringLength], [Range], 和 [RegularExpression]。
 定义自己的定制验证特性,然后应用它们。你可以通过继承自
System.ComponentModel.DataAnnotations命名空间中 的ValidationAttribute基类,定义完全定制的特性。
服务器端校验只需要在Action中校验:ModelState.IsValid属性即可。true就是校验通过,false反之不通过。
要使用客户端验证,必须引入JS脚本支持(jq的校验)
添加语句
<% Html.EnableClientValidation(); %> (MVC3 、4 中默认开启)

@{
        Html.EnableClientValidation(false);
    }
WebConfig,全局配置ClientValidationEnabled,设置为true

<add key="ClientValidationEnabled" value="true" />
2.MVC自带的ajax,先引用jquery和视图ajax,参数参考具体文档

@using (Ajax.BeginForm("GetData""Ajax"new AjaxOptions() { Confirm = "你是否要提交吗",
 HttpMethod = "POST",
 InsertionMode = InsertionMode.Replace,
 UpdateTargetId = "id1", 
OnSuccess = "successFunction",
 LoadingElementId = "loading" }))
       {
            <div>
                用户名<input type="text" name="username" />
                密码<input type="password" name="pwd" />
                <input type="submit" value="提交" />
                         
 
 
            </div>
           <div id="id1"></div>
           <div id="loading"></div>
       }
3.过滤器

微软提供的默认的4种过滤器

Authorizazation

Action filter

Result filter

Exception filter

在验证和动作、视图、异常,当出现某种情况时,就执行该段代码

public class MyActionFilterAttribute : ActionFilterAttribute
    {
        public string Name { getset; }
 
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            HttpContext.Current.Response.Write("<br/>" + Name);
 
        }
    }
可以用过滤器做Session验证,要执行过滤器,只要在控制器或Action上打上标签,对应的Action请求时就
先执行过滤器,如果打在控制器上,里面所有的action都不需要在标志

 [MyActionFilter(Name="home index")]
       public ActionResult Index()
       {
           ViewData["key"= DateTime.Now;
          
           return View();
       }
如果让所有的都要执行,只需在
FilterConfig.cs中注册一个就行,优先级最低
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
       {
           filters.Add(new HandleErrorAttribute());
           filters.Add(new MyActionFilterAttribute() { Name = "global" });
}
 在控制器上加上
 [AttributeUsage(AttributeTargets.All,AllowMultiple=true)]
可以使一个Action执行多个过滤器

4.异常过滤器


public class MyExceptionAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
 
            //当出现错误的时候
            //1.记录日记
            //2.页面跳转到错误或者
            //3.多个线程同时访问一个日志,考虑使用内存队列,为了考虑性能,引入分布式队列Redis
            //4.加入观察者模式,可以写到不同地方,应对不同变化,引入设计模式,写入不同变化点
            //5.log4net
            HttpContext.Current.Response.Redirect("/home/index");
        }
    }
在Filter.cs中配置自己的过滤器
filters.Add(new MyExceptionAttribute());

5.MVC区域功能

Asp.Net MVC提供了区域的功能,可以很方便的为大型的网站划分区域。
可以让我们的项目不至于太复杂而导致管理混乱,有了区域后,每个模块的页面都放入相应的区域内进行管理很方便。
在项目上右击创建新的区域
区域的功能类似一个小的MVC项目,麻雀虽小五脏俱全,有自己的控制器、模型、视图、路由设置
 区域的路由设置是最优先的


6.局部视图和局部渲染

@{Html.RenderPartial("MVCAjax");
         Html.RenderAction("");
       }
页面上做一个嵌套使用html就用frameset和iframe,下载可以用Html.RenderPartial,在webform内使用用
户控件
7.MVC模板页

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值