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
RestrictedAttributeis incorrect. You only need to derive from theActionFilterAttributeclass as that already derives from theIActionFilterinterface.And in your class you need to
overridetheOnActionExecutingof theActionFilterAttributeclass and not implement the interface method i.e. you should not haveIActionFilter.OnActionExecutingas 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.FiltersinAddMvc(). It will work without that :-)