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 7014801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:34:07+00:00 2026-05-27T22:34:07+00:00

I am experimenting with creating my own custom HTTP Context: CustomHttpContext : HttpContextBase {

  • 0

I am experimenting with creating my own custom HTTP Context:

CustomHttpContext : HttpContextBase
{
    public override HttpRequestBase Request { }
}

One thing i can’t figure out is how to initialize the base class with

System.Web.HttpContext.Current

Does anyone have any ideas how i can initialise the custom context first with the Current Http then override certain Methods/Properties to serve my own purpose?

  • 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-27T22:34:08+00:00Added an answer on May 27, 2026 at 10:34 pm

    The simple answer is no, it’s not possible. Also note that HttpContext does not inherit from HttpContextBase, instead, they both implement IServiceProvider. Finally, HttpContext is sealed, suggesting that the authors did not want people to do anything other than consume this class.

    As you are no doubt annoyed by HttpContextBase has a parameterless constructor so does not even give you the option of instantiating it from the current request and response like HttpContext!

    Let’s use a ‘decompiler’ to take a look at the implementation of HttpContext.Current:

    // System.Web.HttpContext
    /// <summary>Gets or sets the <see cref="T:System.Web.HttpContext" /> object for the current HTTP request.</summary>
    /// <returns>The <see cref="T:System.Web.HttpContext" /> for the current HTTP request.</returns>
    public static HttpContext Current
    {
        get
        {
            return ContextBase.Current as HttpContext;
        }
        set
        {
            ContextBase.Current = value;
        }
    }
    

    If we take a look at ContextBase.Current (from System.Web.Hosting.ContextBase):

    // System.Web.Hosting.ContextBase
    internal static object Current
    {
        get
        {
            return CallContext.HostContext;
        }
        [SecurityPermission(SecurityAction.Demand, Unrestricted = true)]
        set
        {
            CallContext.HostContext = value;
        }
    }
    

    and CallContext (in System.Runtime.Messaging):

    // System.Runtime.Remoting.Messaging.CallContext
    /// <summary>Gets or sets the host context associated with the current thread.</summary>
    /// <returns>The host context associated with the current thread.</returns>
    /// <exception cref="T:System.Security.SecurityException">The immediate caller does not have infrastructure permission. </exception>
    public static object HostContext
    {
        [SecurityCritical]
        get
        {
            IllogicalCallContext illogicalCallContext = Thread.CurrentThread.GetIllogicalCallContext();
            object hostContext = illogicalCallContext.HostContext;
            if (hostContext == null)
            {
                LogicalCallContext logicalCallContext = CallContext.GetLogicalCallContext();
                hostContext = logicalCallContext.HostContext;
            }
            return hostContext;
        }
        [SecurityCritical]
        set
        {
            if (value is ILogicalThreadAffinative)
            {
                IllogicalCallContext illogicalCallContext = Thread.CurrentThread.GetIllogicalCallContext();
                illogicalCallContext.HostContext = null;
                LogicalCallContext logicalCallContext = CallContext.GetLogicalCallContext();
                logicalCallContext.HostContext = value;
                return;
            }
            LogicalCallContext logicalCallContext2 = CallContext.GetLogicalCallContext();
            logicalCallContext2.HostContext = null;
            IllogicalCallContext illogicalCallContext2 = Thread.CurrentThread.GetIllogicalCallContext();
            illogicalCallContext2.HostContext = value;
        }
    }
    

    We start to get a feel for how the HttpContext is being retrieved. It’s being packaged in with the thread the current user started when they visted the website (which makes perfect sense!). Delving further we can see it also gets recreated per request (see below).

    We can also see, at the interface layer, HttpContext.Current cannot be changed to point at your own HttpContext as the property is not virtual. It also uses many BCL classes that are private or internal so you can’t simply copy most of the implementation.

    What would be easier, and also less prone to any other issues would be to simply wrap HttpContext with your own CustomContext object. You could simply wrap HttpContext.Current in a BaseContext property, then have your own properties on the class (and use whatever session, database, or request based state storage mechanism you want to store and retrieve your own properties).

    Personally, I’d use my own class for storing my own information, as it belongs to my application and user etc and isn’t really anything to do with the http pipeline or request/response processing.

    See also:

    • ASP.NET MVC : How to create own HttpContext
    • How is HttpContext being maintained over request-response
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm experimenting with creating a custom scope bar that uses recessed-style NSButtonCell objects. However,
How can I create a simple testing/experimenting file in grails? I tried creating a
I am experiencing some performance problems when creating a very simple Python HTTP server.
I'm experimenting with creating a plugin framework for ASP.NET MVC. I've managed to inject
I'm experimenting with creating a commenting system. I'm using php-OEmbed class and HTML Purifier
I'm looking for a simple way to persist objects when experimenting and creating mock
I'm experimenting around with writing custom WinForms components and I wrote a couple of
Currently I am experimenting with Mono, and creating a small testproject. When I tried
I am experimenting with the Aloha inline HTML5 editor (www.aloha-editor.com). I am creating a
I was experimenting with creating new dates in Node.js; for some reason it is

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.