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"