Roman Kiyashev
Roman Kiyashev

Reputation: 160

Inheritance of Action in AsyncController

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

Upvotes: 0

Views: 151

Answers (1)

dotnetspark
dotnetspark

Reputation: 581

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.

Upvotes: 0

Related Questions