How to create a Custom Action Filter in nopCommerce v4

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 Jahre weitere
Just started to upgrade one of our sites to v4.0 and been through the process of upgrading the Action Filters. Thought someone else might interested in this, so I created (my first) a blog post:

http://blog.majako.net/how-to-create-a-custom-action-filter-in-nopcommerce-v4/

Any feedback appreciated!
6 Jahre weitere
Hello,

This is appreciated. We are using this from a long time but it is good to see someone shared a nice written blog on it.
6 Jahre weitere
You can also implement INopStartup

https://www.nopcommerce.com/boards/t/49255/how-can-i-register-a-global-action-filter-from-my-plugin-in-nopcommere-40.aspx
6 Jahre weitere
timmit wrote:
You can also implement INopStartup

https://www.nopcommerce.com/boards/t/49255/how-can-i-register-a-global-action-filter-from-my-plugin-in-nopcommere-40.aspx


Thanks, I updated the post to take this approach instead.
6 Jahre weitere
Very nice, by using the generic filter Add method there is no need to manually instantiate the filter class and DI will handle constructor injection.


public void ConfigureServices(IServiceCollection services, IConfigurationRoot configuration)
        {
            services.Configure<MvcOptions>(config =>
            {              
                config.Filters.Add<ProductDetailsActionFilter>();
            });
        }
6 Jahre weitere
I followed your guide, but the filter is called in all stores even if it is limited to only one of them.

How can I limit the filter to just one store?
5 Jahre weitere
mgustafsson

This link http://blog.majako.net/how-to-create-a-custom-action-filter-in-nopcommerce-v4/

Doesn't work!

Can this be fixed?
Please
5 Jahre weitere
Hemyl wrote:
mgustafsson

This link http://blog.majako.net/how-to-create-a-custom-action-filter-in-nopcommerce-v4/

Doesn't work!

Can this be fixed?
Please


Article is moved to:

http://www.majako.net/how-to-create-a-custom-action-filter-in-nopcommerce-v4/
1 Jahr weitere
=> using action filter we can add our extra logic before action executing or after action executed
=> can check a controller or action from default nop or from plugin

public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext == null || filterContext.HttpContext == null)
                return;

            string controllerName = filterContext?.Controller.ToString();
            if (string.IsNullOrEmpty(controllerName))
                return;

            var actionDescriptor = filterContext.ActionDescriptor as ControllerActionDescriptor;
            string actionName = actionDescriptor?.ActionName;
            if (string.IsNullOrEmpty(actionName))
                return;

            if (((actionDescriptor.ActionName.Equals("PtxInfo", StringComparison.InvariantCultureIgnoreCase) ||
                actionDescriptor.ActionName.Equals("Info", StringComparison.InvariantCultureIgnoreCase)) ||
                (controllerName.Equals("Nop.Web.Areas.Admin.Controllers.CustomerController", StringComparison.InvariantCultureIgnoreCase) ||
                controllerName.Equals("PTX.Nop.Plugin.Custom.Controllers.Admin.PTXCustomerController", StringComparison.InvariantCultureIgnoreCase) &&
                actionDescriptor.ActionName.Equals("Edit", StringComparison.InvariantCultureIgnoreCase))) &&
                filterContext.HttpContext.Request.Method == "POST")                
            {
                var customerEmail = filterContext.HttpContext.Request.Form["Email"];
                if (string.IsNullOrEmpty(customerEmail))
                    return;

                var customer = _customerService.GetCustomerByEmail(customerEmail);
                if (customer == null)
                    return;

                var contactRequest = _activeCampaignHttpClient.PrepareActiveCampaignContactRequest(customer);
                _activeCampaignHttpClient.ActiveCampaignUpdateContact(contactRequest, customer);
            }
        }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.