How can I register a global action filter from my plugin in NopCommere 4.0

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
In previous NopCommerce versions it was straight forward, I would just call `System.Web.Mvc.GlobalFilters.Add` from inside my plugin's custom IStartupTask class like so -


namespace Nop.Plugin.Misc.Custom
{
    public class StartupTask : IStartupTask
    {

        public void Execute()
        {
            System.Web.Mvc.GlobalFilters.Filters.Add(new CustomFilter());
        }

        public int Order
        {
            get { return 0; }
        }

    }

}


With ASP.NET core I have no idea how to achieve this. Can anybody shed some light?

It occurs to me that the way I was doing it probably wasn't a very good way to do it to begin with. I'd like to be able to have services injected into my filter automatically, rather than relying on EngineContext like I was previously.
6 years ago
OK so after extensive Googling I understand the correct way to do it in previous versions of NopCommerce was to use Autofac's AsActionFilterFor method, however this method no longer exists. So how do we do it now?
6 years ago
Hi,

While it is still possible to register action filters in nopCommerce 4.0 you most probably won't need them anymore.
You can create a ModelPrepared consumer and modify the models as described in this post rather than using action filters.

Thanks,
Boyko
6 years ago
The filter in question is used to detect the user hitting any page on the store, so unfortunately the new Model events aren't ideal. I use it for redirection style payment methods. If a user hits any page on the store I cancel the pending order and repopulate their shopping cart with the items from the order. The idea is that if a user gets redirected away to make payment, but then returns to the store to make changes, they are presented with their original shopping cart and are free to make changes. I would be very grateful if you could tell me how to register an action filter.
6 years ago
Hi timmit,

First you need to implement this interface in your plugin INopStartup and set the order to be some number bigger than 2000 for example so that it is called after the default nopCommerce ones.

Then in the ConfigureServices method you need to add this code:

 services.Configure<MvcOptions>(congig => {
                congig.Filters.Add(new YourActionFilter());
            });


You should read a bit more about action filter in asp.net core as there is a recommended way of creating action filters. Also you can see the action filters in nopCommerce as they are following these new recommended way.

Hope this helps!

Thanks,
Boyko
6 years ago
Funnily enough, I initially had set the order to 9999, but it works fine with the order set to 0. Not sure how or why that works, but then a lot of the MVC internals are an absolute mystery to me!
6 years ago
Nop-Templates.com wrote:
Hi timmit,

First you need to implement this interface in your plugin INopStartup and set the order to be some number bigger than 2000 for example so that it is called after the default nopCommerce ones.

....


Been looking for hours for this... Documentation update would be very much appreciated. Hope nopCommerce team sees this. The upgrade guide only tells you to look at the new implemntation, but not on how to actually use it in a plugin... Thanks for your input.
6 years ago
All you had to do was Google it?

http://lmgtfy.com/?q=action+filter+nopcommerce+4.0+plugin
6 years ago
Nop-Templates.com wrote:
Hi timmit,

First you need to implement this interface in your plugin INopStartup and set the order to be some number bigger than 2000 for example so that it is called after the default nopCommerce ones.

Then in the ConfigureServices method you need to add this code:

 services.Configure<MvcOptions>(congig => {
                congig.Filters.Add(new YourActionFilter());
            });


You should read a bit more about action filter in asp.net core as there is a recommended way of creating action filters. Also you can see the action filters in nopCommerce as they are following these new recommended way.

Hope this helps!

Thanks,
Boyko



A slight different way is to use the generic Add method, that way DI will handle instantiation of the filter and resolve the filter's constructor dependencies.

 services.Configure<MvcOptions>(congig => {
                congig.Filters.Add<YourActionFilter>();
            });
6 years ago
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?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.