Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6968921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:30:53+00:00 2026-05-27T16:30:53+00:00

public abstract class MyControllerBase : Controller { protected override void OnActionExecuting(ActionExecutingContext context) { //

  • 0
public abstract class MyControllerBase : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext context)
    {
        // do some magic
    }
}

All of my controllers inherit from MyControllerBase. The problem is that now I can’t unit test certain methods because the filter sets some authorisation/logic flags which influence code path.

Is there any way to manually trigger OnActionExecuting? How does the pipeline trigger these events?

EDIT: to show a little more the idea behind this design in response to comments. I basically have something like this:

public abstract class MyControllerBase : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext context)
    {
        UserProperties = 
             _userService
                .GetUserProperties(filterContext.HttpContext.User.Identity.Name);

        ViewBag.UserProperties = UserProperties;
    }

    public UserProperties { get; private set; }

    public bool CheckSomethingAboutUser()
    {
         return UserProperties != null 
            && UserProperties.IsAuthorisedToPerformThisAction;
    }

    // ... etc, other methods for querying UserProperties
}

So now anywhere in View or Controller I can get details of the current user, what is their email, what authorisation they have, which department they work for etc.

Example:

public class PurchasingController : MyControllerBase 
{
    public ActionResult RaisePurchaseOrder(Item item)
    {
        // can use UserProperties from base class to determine correct action...

        if (UserProperties.CanRaiseOrders)

        if (UserProperties.Department == item.AllocatedDepartment)
    }
}

So this design works really nice, but as you can see testing the above action is difficult as I can’t directly manipulate the UserProperties in the test set up.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:30:54+00:00Added an answer on May 27, 2026 at 4:30 pm

    I’m not sure you’re suppose to override OnActionExecuting like that in MCV, normally I make an ActionFilterAttribute

    public class SomeMagicAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
    
        }
    }
    

    Then your class:

    [SomeMagic]
    public abstract class MyControllerBase : Controller
    {
    
    }
    

    Then in your unit test you can just do

    var magic = new SomeMagicAttribute();
    var simulatedContext = new ActionExecutingContext();
    magic.OnActionExecuting(simulatedContext);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public abstract class Vehicle { protected void SomeMethod<T>(String paramName, ref T myParam, T val)
Take this base class: public abstract class XMPPSubservice { protected XMPPService mTheService; protected XMPPSubservice(Context
public abstract class Master { public void printForAllMethodsInSubClass() { System.out.println (Printing before subclass method
I have a class like so: public abstract class ClassA<T> { protected ClassA(IInterface interface)
Suppose we have abstract class A (all examples in C#) public abstract class A
abstract class SettingSaver { public abstract void add(string Name, string Value); public abstract void
Consider this piece of code: public abstract class Validator { protected Validator() { }
public abstract class A { public abstract void Process(); } public abstract class B
public abstract class Main implements Comparable { public static void main(String[] args) { Integer[]
[InheritedExport(typeof(MyAbstractClass)) public abstract class MyAbstractClass { [Import] protected IFoo Foo { get; private set;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.