Filter in Orchard
When working in Orchard, it’s always a good idea to look at the current codebase as an example.
The one I chose as a reference in creating my custom filter attribute is the Theme
filter.
It is located in $\Orchard\Themes\ThemeFilter.cs
inside the Orchard.Framework
project.
You will notice that ThemeFilter
is actually inherited from a class called FilterProvider
.
|
Now lets have a look inside FilterProvider
.
|
Bingo, it turns out that if you create a class to inherit FilterProvider
and some IActionFilter
, they will be automatically applied to your actions as ActionFilter
already.
Note that Orchard is clever enough to help you to inject your dependencies using Autofac by constructor injection too.
Attribute in Orchard
As you can see, when you created your custom filter, it actually automatically applied to all your actions.
What happanes if you only want to apply your filter in certain actions using attribute?
Its easy, just check the ThemeFilter
and ThemeAttribute
as an example:
- create your own attribute
- check whether the action contains this attribute inside your filter.
- apply what you want if the attribute exists or do nothing if it’s not.