C# 可以先触发基类属性吗?
看起来.NET基类中的属性是在属于继承类的属性之后触发的。对我来说似乎不对劲,还是我做错了什么 我的应用程序中的所有类都继承自具有C# 可以先触发基类属性吗?,c#,asp.net-mvc,inheritance,attributes,polymorphism,C#,Asp.net Mvc,Inheritance,Attributes,Polymorphism,看起来.NET基类中的属性是在属于继承类的属性之后触发的。对我来说似乎不对劲,还是我做错了什么 我的应用程序中的所有类都继承自具有[AuthorizationAttribute]的基类,但只有一些类具有[LoggingAttribute]。 当我在HomeController中访问一个方法时,为什么我的[LoggingAttribute]在授权之前被命中,我如何首先触发基类授权属性,然后再触发日志记录 [AuthorizationAttribute] public class BaseContr
[AuthorizationAttribute]
的基类,但只有一些类具有[LoggingAttribute]
。
当我在HomeController中访问一个方法时,为什么我的[LoggingAttribute]
在授权之前被命中,我如何首先触发基类授权属性,然后再触发日志记录
[AuthorizationAttribute]
public class BaseController
{
//....class methods
}
[LoggingAttribute]
public class HomeController: BaseController
{
//....class methods
}
属性:
public class AuthorizationAttribute : ActionFilterAttribute
{
//...API token gets parsed into Id/Pw
}
public class LoggingAttribute: ActionFilterAttribute
{
//...Logging record saved with Id/pw established above
}
对于这种特定情况,由于它与授权有关,请确保从中派生自定义的
AuthorizationAttribute
,或实现iaauthorizationFilter
在操作筛选器属性之前执行授权筛选器
这保证了控制器操作方法只能在授权时执行。没错,IAuthorizationFilter继承确实允许首先命中基类属性,但我绝对不需要这个额外的逻辑,而且看起来包含的接口ExecuteAuthorizationFilterAsync()方法要求返回在别处生成的HttpResponseMessage。重建这座城市将是一次巨大的检修。还有其他建议吗?@InspiredBy您可以尝试显式设置
顺序
;e、 g.[AuthorizationAttribute(Order=1)]
和[LoggingAttribute(Order=2)]
-我自己从来没有试过。@code受启发,想知道为什么不继承自AuthorizationAttribute
;只有一个bool
可以返回。