1 /// <summary>2 /// 3 /// </summary>4 Public classAjaxonlyattribute:actionfilterattribute5 {6 /// <summary>7 ///called by the ASP. NET MVC Framework before the action method executes.8 /// </summary>9 /// <param name= "Filtercontext" >The filter context.</param>Ten Public Override voidonactionexecuting (actionexecutingcontext filtercontext) One { A if(!filterContext.HttpContext.Request.IsAjaxRequest ()) - { -Filtercontext.result =NewContentresult () {Content ="only Ajax method calls are supported. ", contentencoding = Encoding.UTF8, ContentType ="text/html" }; the //new Httpnotfoundresult (); - } - Base. OnActionExecuting (filtercontext); -}only allow calls through Ajax
1 /// <summary>2 ///Customizing template pages3 /// </summary>4 Public classLayoutattribute:actionfilterattribute5 {6 Private ReadOnly string_mastername;7 PublicLayoutattribute (stringmastername)8 {9_mastername =Mastername;Ten } One /// <summary> A ///execution after page rendering finishes - /// </summary> - /// <param name= "Filtercontext" ></param> the Public Override voidonactionexecuted (actionexecutedcontext filtercontext) - { - Base. OnActionExecuted (filtercontext); - varresult = Filtercontext.result asViewResult; + if(Result! =NULL) - { +Result. Mastername =_mastername; A } at } -}set up a common motherboard page
1 /// <summary>2 ///Page Title3 /// </summary>4 Public classPagetitleattribute:actionfilterattribute5 {6 Private ReadOnly string_pagetitle;7 PublicPagetitleattribute (stringpagetitle)8 {9_pagetitle =PageTitle;Ten } One /// <summary> A ///execution after page rendering finishes - /// </summary> - /// <param name= "Filtercontext" ></param> the Public Override voidonactionexecuted (actionexecutedcontext filtercontext) - { - Base. OnActionExecuted (filtercontext); - varresult = Filtercontext.result asViewResult; + if(Result! =NULL) - { +Result. Viewbag.title =_pagetitle; A } at } -}Set the action page title
1[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, AllowMultiple =false)]2 Public classAuthorizefilterattribute:actionfilterattribute3 {4 //called by the ASP. OnActionExecuted after executing the action method. 5 //called by the ASP. OnActionExecuting before executing the action method. 6 //called by the ASP. Onresultexecuted after executing the result of the operation. 7 //Onresultexecuting is called by the ASP. NET before executing the result of the operation. 8 Public Override voidonactionexecuting (actionexecutingcontext filtercontext)9 {Ten //dosomething (); One } A}Set Page access validation
Common filtering attribute classes in ASP. Net MVC