I have a restricted attribute class in Asp.net Core 2.0 project and I decorate my controller with this attribute but this class is not executing when I run the project I also put debug point on it here I am register this attribute
services.AddMvc(config =>
{
config.Filters.Add(typeof(RestrictedAttribute));
});
[Area("Admin")]
[Restricted]
public class MediaController : Controller
{}
public class RestrictedAttribute : ActionFilterAttribute, IActionFilter
{
void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
{ //Other Code } }
The declaration for your
RestrictedAttribute
is incorrect. You only need to derive from theActionFilterAttribute
class as that already derives from theIActionFilter
interface.And in your class you need to
override
theOnActionExecuting
of theActionFilterAttribute
class and not implement the interface method i.e. you should not haveIActionFilter.OnActionExecuting
as that is not the method that needs to be implemented.The correct implementation is:
In addition, you don't need to add the filter to the
config.Filters
inAddMvc()
. It will work without that :-)