Common filtering attribute classes in ASP. Net MVC

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.