Varinder
Varinder

Reputation: 1910

OnActionExecution on BaseController and Particular Controller

I have a BaseController which is like below.

 public class BaseController : Controller
{
    
    public string BDynamicConnectionString { get; set; }
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // Getting dynamic connectionstring
        this.BDynamicConnectionString = GetDynamicConnetionString(this.BCCompanyId, this.BCCompanyIdentifier);

    }
}

I have inherited this base controller in my controller as below.

public class TestController : BaseController
{
    private ClassroomBL objClassroomBL;

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        this.objClassroomBL = ClassObjects.ClassroomBLObject(BDynamicConnectionString);
    }

    public ActionResult FetchDefaultStatuses()
    {
        this.objClassroomBL.FetchDefaultStatuses(145);
        return View();
    }
}

As it is visible I have OnActionExecuting(ActionExecutingContext filterContext) on both BaseController and TestController. Actually Why I did it like this is I am managing many things in base controller which are needed in each controller inherited from this.

But now I need something like OnActionExecuting(ActionExecutingContext filterContext) on particular controller because I have to initialize object before calling action and object which I have to initialize it uses a property which is initialized in BaseController.

If I am adding OnActionExecuting(ActionExecutingContext filterContext) on both then first, controller which is inheriting BaseController on this event fires before the BaseController. I know this happening because of OOPS concept.

Is anything else I can add on Controller which fires after Basecontrollers OnActionExecuting but before action execution of particular controller .

Thanks!

Upvotes: 0

Views: 463

Answers (1)

Alexander
Alexander

Reputation: 4537

Just use following in the first line of overrided method:

base.OnActionExecuting(filterContext)

See also base keyword.

Upvotes: 1

Related Questions