MVC中Action 过滤

总结Action过滤器实用功能,常用的分为以下两个方面:

1、Action过滤器主要功能就是针对客服端请求过来的对象行为进行过滤,类似于门卫或者保安的职能,通过Action过滤能够避免一些非必要的深层数据访问。创建Action过滤的类继承与命名空间Sytem.Web.MVC下的ActionFilterAttribute。举例代码如下:

public class AdminFilter:ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //base.OnActionExecuting(filterContext);判断当前用户的Session是不是管理员
            if (!Convert.ToBoolean(filterContext.HttpContext.Session["IsAdmin"]))
            {
                filterContext.Result = new ContentResult()
                {
                    Content="Unauthorized to access specified resource"
                };
            }
        }
    }

引用该自定义的Action过滤的代码(主要是Action)如下:

       [AdminFilter]
        public ActionResult AddNew()
        {
            EmployeeViewModel myple = new EmployeeViewModel();
            //myple.FooterData = new FooterViewModel();
            //myple.FooterData.CompanyName = "StepByStepSchools";
            //myple.FooterData.Year = DateTime.Now.Year.ToString();
            //myple.UserName = User.Identity.Name; ;
            return View("CreateEmployee", myple);
        }

2、通过Action请求进行数据的操作。

场景表述如下:Part分部视图的时候,视图是强数据类型,part中的数据,需要通过每个分页面获取,这个时候,每个分页面都需要定义Part视图需要参数,那么怎么才能不用设置分视图处理这样的数据那?当然继承是一种传播Part视图需要参数的一种方式,但是继承的数据需要实例化。这个时候,我们可以利用Action过滤的方式来完成。ActionFileter代码如下:

public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            //base.OnActionExecuted(filterContext);
            ViewResult v = filterContext.Result as ViewResult;
            if (v != null)
            {
                BaseViewModel bvm = v.Model as BaseViewModel;
                if (bvm != null)
                {
                    bvm.UserName = HttpContext.Current.User.Identity.Name;
                    bvm.FooterData = new FooterViewModel();
                    bvm.FooterData.CompanyName = "StepByStepSchools";
                    bvm.FooterData.Year = DateTime.Now.Year.ToString();
                }
            }
        }

该代码将BaseViewModel进行数据填充,来显示数据。

Control中Action使用的情况如下:

[HeaderFooterFilter]
        [Authorize]
        public ActionResult Index()
        {

            EmployeeBusinessLayer empBal = new EmployeeBusinessLayer();
            List<Employee> employees=empBal.GetEmployees();
        ....................................
}
时间: 2024-08-26 19:40:03

MVC中Action 过滤的相关文章

NHibernate中Session与ASP.NET MVC中Action的综合使用

NHibernate的基本特征是完成面向对象的程序设计语言到关系数据库的映射,在NHibernate中使用持久化对象PO(Persistent Object)完成持久化操作,对PO的操作必须在Session管理下才能同步到数据库, 但是这里的Session并非指HttpSession,可以理解为基于ADO.NET的Connnection,Session是NHibernate运作的中心,对象的生命周期.事务的管理.数据库的存取都与Session息息相关,首先,我们需要知道, SessionFact

MVC中Action的执行过程

接着上一篇:MVC控制器的激活过程 一.代码现行,该伪代码大致解析了Action的执行的过程 try { Run each IAuthorizationFilter's OnAuthorization() method if(none of the IAuthorizationFilters cancelled execution) { Run each IActionFilter's OnActionExecuting() method Run the action method Run ea

MVC中Action参数绑定的过程

一.题外话 上一篇:MVC中Action的执行过程 ControllerContext 封装有了与指定的 RouteBase 和 ControllerBase 实例匹配的 HTTP 请求的信息. 二.Model绑定者 2.1相关说明 http请求中的参数绑定到Model,是由实现了IModelBinder的类来完成的.我们称这样的类叫做Model绑定者 using System; namespace System.Web.Mvc { /// <summary>Defines the metho

Asp .Net MVC中常用过滤属性类

1 /// <summary> 2 /// 3 /// </summary> 4 public class AjaxOnlyAttribute : ActionFilterAttribute 5 { 6 /// <summary> 7 /// Called by the ASP.NET MVC framework before the action method executes. 8 /// </summary> 9 /// <param name=

Asp.Net MVC中Action跳转

首先我觉得action的跳转大致可以这样归一下类,跳转到同一控制器内的action和不同控制器内的action.带有参数的action跳转和不带参数的action跳转. 一.RedirectToAction("Index");//一个参数时在本Controller下,不传入参数. 二.RedirectToAction(ActionName,ControllerName) //可以直接跳到别的Controller. 三.RedirectToRoute(new {controller=&q

ASP.NET MVC中Action实现页面跳转

1.return RedirectToAction(ActionName);//同一个Controller内 2.RedirectToAction(ActionName,ControllerName);//可以跳转到其他Controller 3.return RedirectToRoute(new {controller=ControllerName,action=ActionName});//可以跳转到其他Controller 4.Response.Redirect(ActionName);/

MVC中前台如何向后台传递数据------$.get(),$post(),$ajax(),$.getJSON()总结

一.引言 MVC中view向controller传递数据的时候真心是一个挺让人头疼的一件事情.因为原理不是很懂只看一写Dome,按葫芦画瓢只能理解三分吧. 二.解读Jquery个Ajax函数 $.get(),$.post(),$.ajax(),$.getJSON() 1.$.get(url,[data],[callback]) 参数说明 url:请求地址,MVC中一般为:“/QueryScores/Search/” (/controller/action/) data:请求数据列表,MVC中ac

ASP.NET MVC中使用Dropzone.js实现图片的批量拖拽上传

说在前面 最近在做一个MVC相册的网站(这里),需要批量上传照片功能,所以就在网上搜相关的插件,偶然机会发现Dropzone.js,试用了一下完全符合我的要求,而且样式挺满意的,于是就在我的项目中使用了这个插件.在使用的过程中发现中文的相关文档较少,说多了都是泪,硬着头皮看官方的网站,本来英文不咋地,只能边查单词边用了,于是就有了这篇文章,主要是总结在使用Dropzone中的遇到的一些问题及详细的使用步骤. Dropzone.js是啥? Dropzone.js是一个开源库,提供拖放文件上传及图像

ASP.NET MVC中的Session以及处理方式

最近在ASP.NET MVC项目中碰到这样的情况:在一个controller中设置了Session,但在另一个controller的构造函数中无法获取该 Session,会报"System.NullReferenceException"错误.之所以这样做是因为希望在controller构造函数中获 取Session值并赋值给一个全局变量,好让该controller的多个action共享. 起先以为是ASP.NET State Service服务没有开启,或者是Web.config中se