0

I am working on a Asp.net MVC project and I am wondering if there is a way for the attributes to talk to other attributes.

For example, I have the following attributes

public class SuperLoggerAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        //Do something super
    }
}

public class NormalLoggerAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        //Do something normal ONLY if the super attribute isn't applied 
    }
}

And I have the following controllers

[NormalLogger]
public class NormalBaseController : Controller
{

}

public class PersonController: NormalBaseController
{

}

[SuperLogger]
public class SuperController: NormalBaseControler
{

}

So basically, I want my SuperController to use SuperLogger and ignore NormalLogger (which was applied in the base), and PersonController should use the NormalLogger since it's not "overrided" by the SuperLogger. Is there a way to do that?

Thanks,

Chi

2 Answers 2

1

Why not just have SuperLoggerAttribute inherit from NormalLoggerAttribute and override the Log method?

Sign up to request clarification or add additional context in comments.

9 Comments

-1: By doing this, SuperController would have both the NormalLoggerAttribute and the SuperLoggerAttribute applied, which is not what Chi Chan intended to do.
Does SuperController not already have both, by virtue of extending an attributed base class?
Thank you arootbeer. But by doing so I will have to Add [NormalLogger] on PersonController. I want to avoid doing that because I have a lot of controllers that inherit from the base and I only have one "Super" controller that I want a different behavior with.
Why would you have to add NormalLogger on PersonController? It would still inherit the NormalLogger attribute from NormalController as it's doing now.
I have just tried this. If you keep [NormalLogger] on BaseController, SuperController will execute NormalLogger (defined in BaseController), and then it will execute SuperLogger (defined in SuperController). I can inherit SuperLogger from NormalLogger to save code, but it won't stop it from executing NormalLogger.
|
0

I think this should work:

public enum LoggerTypes
{
    Normal,
    Super
}

public class LoggerAttribute : ActionFilterAttribute
{
    public LoggerAttribute() : base()
    {
        LoggerType = LoggerTypes.Normal;
    }

    public LoggerAttribute(LoggerTypes loggerType) : base()
    {
        LoggerType = loggerType;
    }

    public LoggerTypes LoggerType
    {
        get; 
        set;
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (LoggerType == LoggerTypes.Super)
        {
            //
        }
        else
        {
            //
        }
    }

Usage:

[Logger(LoggerTypes.Normal)]

or

[Logger(LoggerTypes.Super)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.