ASP.NET MVC(4)

来源:互联网 发布:elementary os ubuntu 编辑:程序博客网 时间:2024/05/21 17:49

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.ReplaceUpdateTargetId = "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
原创粉丝点击