Kurser och utbildning av IT-proffs och systemutvecklare

i Sök

Cornerstones utvecklarblogg

Fluent-API to add ActionFilters to Controllers – ASP.NET MVC Part 3

The latest source code for my Fluent-API to add Action Filters to Controllers is no available here. If you haven’t read about my test project, you can find the other posts here:

Fluent-API to add ActionFilters to Controller in ASP.NET MVC

Fluent-API to add ActionFilters to Controllers – ASP.NET MVC Part 2

To use the Fluent-API, you only need to change the Default Controller factory to the ActionFilterConfigControllerFactory, then just configure your controller within the Global.asax. Here is an example where the OutputCache is added to two Action methods, and also an example how to add one or many ActionFilters to one Action method. You can also add Action Filters on a Controller level so they will be used on every Action methods. If you also want a specific Action Filter for all Controllers, you can just Configure the Controller type and add the filters to it.

protected void Application_Start()
{
            ControllerBuilder.Current.SetControllerFactory(typeof(ActionFilterConfigControllerFactory));

            RegisterRoutes(RouteTable.Routes);
            RegisterActionFilters();
}

        
private void RegisterActionFilters()
{
            // Add OutputCache to all Action Methods for all Controllers which inherits the Contoller class
            //ConfigActionFilter.ConfigController<Controller>()
            //  .AddFilterToController(new OutputCacheAttribute() { Duration = 10, VaryByParam = "none" });

            ConfigActionFilter.ConfigController<HomeController>()
                            .AddFilterToController(new HandleErrorAttribute())
                            .AddFilterToActions(new OutputCacheAttribute() { Duration = 10, VaryByParam = "none" },
                                                c => c.About(),
                                                c => c.Index());

            ConfigActionFilter.ConfigController<AccountController>()
                              .AddFilterToAction(c => c.LogOn(), 
new HandleErrorAttribute(),
new MyCustomFilterAttribute()); }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

This is as I wrote only a test project, and I do like the idea to add cross-cutting concerns by not adding attributes directly to the Controllers source code.

Publicerad den 10 november 2009 19:04 av Fredrik Normén
Taggad med:

Kommentarer

Inga kommentarer
Anonyma kommentarer är avaktiverat

Den här bloggen

Syndication