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

  • SEARCH
  • Home
  • 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 7083395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:10:21+00:00 2026-05-28T07:10:21+00:00

I have been doing some testing with the following code to try and work

  • 0

I have been doing some testing with the following code to try and work out how ActionFilterAttributes works:

public class TestAttribute : ActionFilterAttribute
{
    private string _privateValue;
    public string PublicValue { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        _privateValue = DateTime.Now.ToString();

        base.OnActionExecuting(filterContext);
    }
}

When I run the above code on two parallel threads the _privateValue field gets confused. However the PublicValue property doesn’t get confused.

It looks to me like ActionFilterAttributes are reused across threads, but that new instances are created depending on the constants specified to public properties. Am I correct?

Where can I find information on this?

  • 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-28T07:10:22+00:00Added an answer on May 28, 2026 at 7:10 am

    This will depend on the version of ASP.NET MVC but you should never store instance state in an action filter that will be reused between the different methods. Here’s a quote for example from one of the breaking changes in ASP.NET MVC 3:

    In previous versions of ASP.NET MVC, action filters are create per
    request except in a few cases. This behavior was never a guaranteed
    behavior but merely an implementation detail and the contract for
    filters was to consider them stateless. In ASP.NET MVC 3, filters are
    cached more aggressively. Therefore, any custom action filters which
    improperly store instance state might be broken.

    This basically means that the same instance of the action filter can be reused for different actions and if you have stored instance state in it it will probably break.

    And in terms of code this means that you should absolutely never write an action filter like this:

    public class TestAttribute : ActionFilterAttribute
    {
        private string _privateValue;
    
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            _privateValue = ... some calculation
        }
    
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            // use _privateValue here
        }
    }
    

    but you should write it like this:

    public class TestAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var privateValue = ... some calculation
            filterContext.HttpContext.Items["__private_value__"] = privateValue;
        }
    
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var privateValue = filterContext.HttpContext.Items["__private_value__"];
            // use privateValue safely here
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been doing some testing with ObjectiveResource (iOS->Rails bridge). Things seem to work,
I have been doing some testing on the Random class and I have used
I've been doing some work testing web applications with Cucumber and I currently have
I have been doing some testing with the differences between JavaScript and PHP regular
I have been doing some testing whilst using Jquery UI datepicker and have found
I have been doing some unsafe bitmap operations and have found out that increasing
I have been doing some mobile web app testing and have noticed that the
We have been doing some research into physically isolating the secure and non-secure sections
I have been doing some research on test driven development and find it pretty
I've been doing some Web-Projects lately that rely on heavy Data-Binding and have been

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.