My custom filter is not executing

Topics: Customizing Orchard, Troubleshooting, Writing modules, Writing themes
Feb 20, 2015 at 2:42 PM
I'm trying to create my own little custom filter for my theme, following this examples here:
but visual studio does not seem to hit the constructor in my filter class at all when I'm in debugging mode, and the outcome of OnResultExecuting is not applied. This is the same issue as in the first link here, and no, I don't wan't to use alternates for my purpose

I have generated a visual studio project with .csproj file using code generator.

Orchard version is 1.8.1

Does this work at all on themes or do I miss something here?
Developer
Feb 20, 2015 at 4:51 PM
Is your filter public? Is its module enabled? What do you mean exactly by "I have generated a visual studio project with .csproj file using code generator.", did you just create a class library project and added it? Does your module has a Module.txt? Can you show the code?
Feb 20, 2015 at 6:06 PM
Thank you for you response.

I used orchards codegen tool to create the theme, with the following command (this went just as expected, theme was created and added to solution. The theme is also set active and works as expected):
Codegen theme CrmBlueResp /Createproject:true /IncludeInsolution:true
Since this is a theme, I do not have module.txt, but I do have theme,txt

This is a screen dump of my theme in vs: screendump
using System.Web.Mvc;
using Orchard;
using Orchard.Mvc.Filters;

namespace CrmBlueResp.Filters
{
    public class LayoutFilter : FilterProvider, IResultFilter {

        private readonly IWorkContextAccessor _wca;

        public LayoutFilter(IWorkContextAccessor wca) {
            _wca = wca;
        }

        public void OnResultExecuting(ResultExecutingContext filterContext) {
            var workContext = _wca.GetContext();
            var routeValues = filterContext.RouteData.Values;

            if (routeValues["area"].ToString() == "HomePage") {
                workContext.Layout.IsHomepage = true;
            }
        }

        public void OnResultExecuted(ResultExecutedContext filterContext) {

        }
    }
}
Developer
Feb 20, 2015 at 6:46 PM
I see. Nothing seems wrong here, including that you correctly have a theme project (C# code in folder-themes won't run).

Is there nothing suspicious in the log?
Feb 20, 2015 at 8:20 PM
If you are referring to log in App_data /logs (orchard\src\Orchard.Web\App_Data\Logs) I can not see anything of value. Last orchard-error log generated was @14:17. Now it is 21:18 and no errors. Here is the entire log file: (Btw I fixed the issue in the log with operand !)
2015-02-20 14:17:16,424 [68] Orchard.Exceptions.DefaultExceptionPolicy - Default - An unexpected exception was caught
 http://localhost/orchard
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Operator '!' cannot be applied to operand of type 'Orchard.UI.Zones.ZoneOnDemand'
   at CallSite.Target(Closure , CallSite , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at ASP._Page_Themes_orchardthemes_crmblueresp_Views_Layout_cshtml.Execute() in e:\Git\orchard\src\Orchard.Web\Themes\orchardthemes-crmblueresp\Views\Layout.cshtml:line 154
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
   at Orchard.DisplayManagement.Descriptors.ShapeTemplateStrategy.ShapeTemplateBindingStrategy.Render(ShapeDescriptor shapeDescriptor, DisplayContext displayContext, HarvestShapeInfo harvestShapeInfo, HarvestShapeHit harvestShapeHit) in e:\Git\orchard\src\Orchard\DisplayManagement\Descriptors\ShapeTemplateStrategy\ShapeTemplateBindingStrategy.cs:line 151
   at Orchard.DisplayManagement.Descriptors.ShapeTemplateStrategy.ShapeTemplateBindingStrategy.<>c__DisplayClass26.<>c__DisplayClass28.<Discover>b__15(DisplayContext displayContext) in e:\Git\orchard\src\Orchard\DisplayManagement\Descriptors\ShapeTemplateStrategy\ShapeTemplateBindingStrategy.cs:line 133
   at Orchard.DisplayManagement.Descriptors.ShapeAlterationBuilder.<>c__DisplayClass3.<>c__DisplayClass5.<BoundAs>b__2(DisplayContext displayContext) in e:\Git\orchard\src\Orchard\DisplayManagement\Descriptors\ShapeAlterationBuilder.cs:line 56
   at Orchard.DisplayManagement.Implementation.DefaultDisplayManager.Process(ShapeBinding shapeBinding, IShape shape, DisplayContext context) in e:\Git\orchard\src\Orchard\DisplayManagement\Implementation\DefaultDisplayManager.cs:line 185
   at Orchard.DisplayManagement.Implementation.DefaultDisplayManager.Execute(DisplayContext context) in e:\Git\orchard\src\Orchard\DisplayManagement\Implementation\DefaultDisplayManager.cs:line 94
   at Orchard.DisplayManagement.Implementation.DisplayHelper.ShapeExecute(Object shape) in e:\Git\orchard\src\Orchard\DisplayManagement\Implementation\DisplayHelper.cs:line 86
   at Orchard.DisplayManagement.Implementation.DisplayHelper.Invoke(String name, INamedEnumerable`1 parameters) in e:\Git\orchard\src\Orchard\DisplayManagement\Implementation\DisplayHelper.cs:line 44
   at Orchard.DisplayManagement.Implementation.DisplayHelper.TryInvoke(InvokeBinder binder, Object[] args, Object& result) in e:\Git\orchard\src\Orchard\DisplayManagement\Implementation\DisplayHelper.cs:line 29
   at CallSite.Target(Closure , CallSite , Object , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at Orchard.Mvc.ViewEngines.ThemeAwareness.LayoutAwareViewEngine.<>c__DisplayClass7.<FindView>b__5(ViewContext viewContext, TextWriter writer, IViewDataContainer viewDataContainer) in e:\Git\orchard\src\Orchard\Mvc\ViewEngines\ThemeAwareness\LayoutAwareViewEngine.cs:line 63
   at Orchard.Mvc.ViewEngines.ThemeAwareness.LayoutAwareViewEngine.LayoutView.Render(ViewContext viewContext, TextWriter writer) in e:\Git\orchard\src\Orchard\Mvc\ViewEngines\ThemeAwareness\LayoutAwareViewEngine.cs:line 90
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
Developer
Feb 21, 2015 at 9:31 AM
I've experienced this once before myself, and the issue was that the feature name of the theme as stored in the database tables Settings_ShellFeatureRecord and Settings_ShellFeatureStateRecord did not match the actual implicit feature name of the theme. I don't recall how that happened, just that it happened and the fix was simple. You need to ensure the theme's (implicit) feature is enabled. The implicit feature name is the same as the theme's technical name aka extension ID (not the friendly name as defined in the manifest). The extension ID is the name of the theme's folder.
Marked as answer by kurt_im on 3/5/2015 at 11:15 AM
Feb 21, 2015 at 8:15 PM
Edited Feb 21, 2015 at 8:20 PM
Tnx, that seem to do the trick. My theme's folder name was not the same as the vsiual studio project name and name in the manifest (to clear up why; I'm storing the code in bitbucket and to categories my projects in bitbucke I did add a prefix to the repo name. When cloning this repo, the bitbucket name with prefix was cloned as root folder ).

So to sum up:

My themes folder name, feature name stored in Settings_ShellFeatureRecord and Settings_ShellFeatureStateRecord was:
orchardthemes-crmblueresp
while my visualstudio project name was:
crmblueresp
Syncing this two names to the same name (crmblueresp) I managed to hit breakpoint and debug my custom filter.

The funny thing though was that
routeValues["area"].ToString() == "HomePage" //false, "HomePage does not exist
is not HomePage, but is "Contents
routeValues["area"].ToString() == "Contents" //true
but anyways I leave that to an other thread topic.