MVC Small Series (16) "Dynamically set the template page (Layout) at the controller level or at the specific action level"

Source: Internet
Author: User

The method is to use: ActionFilterAttribute
Several of its methods: Onactionexecuted,onactionexecuting,onresultexecuted and Onresultexecuting, which record an action from loading to a page that eventually shows up in the browser's entire process, This thing is generally used in page permissions verification, Layout page control
Here are several methods to execute the order: onactionexecuting action before execution
onactionexecuted action after execution
Onresultexecuting after page rendering
onresultexecuted Page Rendering Results

You can write a derived class by using the ActionFilterAttribute feature, and then go to overwrite the onactionexecuted
Modify the Layout page (note that the layout page is already the default after the action has been executed)

 /// <summary>    ///Customizing template pages/// </summary>     Public classLayoutattribute:actionfilterattribute {Private ReadOnly string_mastername;  PublicLayoutattribute (stringmastername) {_mastername=Mastername; }        /// <summary>        ///execution after page rendering finishes/// </summary>        /// <param name= "Filtercontext" ></param>         Public Override voidonactionexecuted (ActionExecutedContext filtercontext) {Base.            OnActionExecuted (Filtercontext); varresult = Filtercontext.result asViewResult; if(Result! =NULL) {result. Mastername=_mastername; }        }    }
View Code

Finally apply it to the Controller or action, using very simple

[Filters.layout ("_layouted")]     Public classHomecontroller:controller { PublicActionResult Index () {viewbag.message="Modify This template to jump-start your ASP application."; returnView (); } [Filters.layout ("_layouted")]         PublicActionResult About () {Viewbag.message="Your App description page."; returnView (); }         PublicActionResult Contact () {Viewbag.message="Your Contact page."; returnView (); }    }
View Code

MVC Small Series (16) "Dynamically set the template page (Layout) at the controller level or at the specific action level"

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.