我有一个基础AsyncController
BaseController : AsyncController
{
[Authorize("Admin")]
public virtual async Task<ActionResult> SomeMethod()
{
//code
}
}
如何正确继承并覆盖 SomeMethod ?
UserController : BaseController
{
[Authorize("User")]
public override Task<ActionResult> SomeMethod()
{
return base.SomeMethod()
}
}
或
UserController : BaseController
{
[Authorize("User")]
public override async Task<ActionResult> SomeMethod()
{
return await base.SomeMethod()
}
}
P.S。对不起我的英文
答案 0 :(得分:0)
SomeMethod()
在您的BaseController
课程中定义。因此,子类应继承自BaseController
而不是AsyncController
。要覆盖该方法,只需在定义中添加virtual
关键字即可。
BaseController : AsyncController
{
[Authorize("Admin")]
public virtual async Task<ActionResult> SomeMethod()
{
//code
}
}
继承和覆盖
UserController : BaseController
{
[Authorize("User")]
public override Task<ActionResult> SomeMethod()
{
return base.SomeMethod()
}
}
我也注意到Authorize
属性中有些奇怪的东西。如果基本方法仅允许Admin,并且子方法仅允许User,那么您很可能最终无法从子类执行base方法。