Take the 2-minute tour ×

My idea was to create my own HttpContext which will include some of the internal objects used in our app. So I thought I will simply create

public class FooHttpContextBase : HttpContextBase
    public string Foo
        get { return "Boo"; }

And then override HttpContext property:

public abstract class BaseController : Controller
    private FooHttpContextBase context;

    public BaseController()
        context = new FooHttpContextBase();

    override public HttpContextBase HttpContext
        get { return context; }

But then I've realized HttpContext is not a virtual - so it cannot be overridden.

Well, what do you suggest? Add some new property into the BaseController?

Thanks in advance!

share|improve this question
Even if you could inject your own HttpContext descendant, what good would that do? What do you intend to put in it? –  Aaronaught Feb 19 '10 at 20:23
Are you trying to implement global settings for all sessions or session specific settings? –  Todd Smith Feb 19 '10 at 20:31
It depends entirely on exactly what you're storing, and you haven't told us anything about that. There are lots of places to squirrel data away. Which one is right depends upon how the data is to be used. –  Craig Stuntz Feb 19 '10 at 20:39
I'd like to store some session specific objects. First which come into my mind is Account object used in our app. Or some other 'session specific' objects. –  palig Feb 19 '10 at 20:43
Session and membership are almost completely unrelated. Security-sensitive information should always be in a principal rather than session. Session is insecure. So again, you need to be precise about what you're storing. –  Craig Stuntz Feb 19 '10 at 22:21
add comment

1 Answer

up vote -6 down vote accepted

I recommend you just add a new property to your base controller that will share your own context. It's the most easy, elegant and fast way to do the job.
If you are using some IOC, I recommend you to do a property injection for it, see this link for details.

share|improve this answer
Yes, if this is the best way how to do that then it's ok for me. –  palig Feb 19 '10 at 21:00
-1 There is no need to be crass. If you have an answer to the question, simply state the solution. –  rusmus Jan 16 at 15:58
add comment

Your Answer


