我正在开发一个Asp.net MVC项目,我想知道是否有一种方法可以让属性与其他属性进行对话。
例如,我有以下属性
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
}
}
我有以下控制器
[NormalLogger]
public class NormalBaseController : Controller
{
}
public class PersonController: NormalBaseController
{
}
[SuperLogger]
public class SuperController: NormalBaseControler
{
}
基本上,我希望我的SuperController使用SuperLogger并忽略NormalLogger(在基础中应用),而PersonController应该使用NormalLogger,因为它不会被SuperLogger“覆盖”。有没有办法做到这一点?
谢谢,
志
答案 0 :(得分:1)
为什么不SuperLoggerAttribute
继承NormalLoggerAttribute
并覆盖Log
方法?
答案 1 :(得分:0)
我认为这应该有效:
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
{
//
}
}
用法:
[Logger(LoggerTypes.Normal)]
或
[Logger(LoggerTypes.Super)]