SomeMethod() { //code } " /> SomeMethod() { //code } " /> SomeMethod() { //code } "/>

Inheritance of Action in AsyncController

153 Views Asked by At

I have a base AsyncController

BaseController : AsyncController
{ 
    [Authorize("Admin")]
    public virtual async Task<ActionResult> SomeMethod()
    {
        //code
    }
}

How it is correct to inheritance and override SomeMethod?

UserController : BaseController
{ 
    [Authorize("User")]
    public override Task<ActionResult> SomeMethod()
    {
        return base.SomeMethod()
    }
}

OR

UserController : BaseController
{ 
    [Authorize("User")]
    public override async Task<ActionResult> SomeMethod()
    {
        return await base.SomeMethod()
    }
}

P.S. Sorry for my english

1

There are 1 best solutions below

0
dotnetspark On

SomeMethod() is defined within your BaseController class. Therefore child classes should inherit from BaseController rather than AsyncController. To override the method, just add virtual keyword into the definition.

BaseController : AsyncController
{ 
    [Authorize("Admin")]
    public virtual async Task<ActionResult> SomeMethod()
    {
        //code
    }
}

Inheritance and override

UserController : BaseController
{ 
    [Authorize("User")]
    public override Task<ActionResult> SomeMethod()
    {
        return base.SomeMethod()
    }
}

Also I have noticed something odd in your Authorize attributes. If the base method is allow to Admin only and the child method is allow to User only, then you most likely end up being unable to execute the base method from the child class.