Accessing Cache from Custom Web Server Control

Discussion in 'ASP .Net' started by Jordan S., May 30, 2008.

  1. Jordan S.

    Jordan S. Guest

    In a "standard" aspx page I can access the system cache
    (System.Web.Caching.Cache), like this:
    Cache["SomeObject"] = someObject;

    How can I access the same Cache from *within* a custom composite Web server
    control I'm writing (that lives in it's own assembly) that is dynamically
    added to a page at runtime? What do I need to do with the custom control or
    it's containing assembly to enable it to access the Cache of the hosting Web
    application?

    Thanks.
    Jordan S., May 30, 2008
    #1
    1. Advertising

  2. Hello Jordan S.

    > How can I access the same Cache from *within* a custom composite Web
    > server control I'm writing (that lives in it's own assembly) that is
    > dynamically added to a page at runtime? What do I need to do with the
    > custom control or it's containing assembly to enable it to access the
    > Cache of the hosting Web application?

    Two Ways:

    HttpContext.Current.Cache

    this.Page.Cache

    both gives the same Reference to the current Cache Object.

    --
    Gruss, Peter Bucher
    Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
    http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
    Peter Bucher [MVP], May 31, 2008
    #2
    1. Advertising

  3. Jordan S.

    Jordan S. Guest

    "Peter Bucher [MVP]" <> wrote in message
    news:...
    > Hello Jordan S.
    >
    >> How can I access the same Cache from *within* a custom composite Web
    >> server control I'm writing (that lives in it's own assembly) that is
    >> dynamically added to a page at runtime? What do I need to do with the
    >> custom control or it's containing assembly to enable it to access the
    >> Cache of the hosting Web application?

    > Two Ways:
    >
    > HttpContext.Current.Cache
    >
    > this.Page.Cache
    >
    > both gives the same Reference to the current Cache Object.




    Unfortunately neither of your suggestions works in my case. I want to access
    the Cache from *within* a web server control. There is no "this.Page"
    reference in a web server control.

    The suggestion for HttpContext.Current.Cache seems promising, but it's
    currently not available in my control's class.

    Am I just missing some reference? The project/assembly within which my
    control exists references System.Web. What else do I need to reference - or
    what do I need to do to be able to access teh current context from within a
    custom control?

    Thanks.
    Jordan S., May 31, 2008
    #3
  4. Jordan S.

    Joe Guest

    Your just missing a reference. HttpContext.Current.Cache is the same
    object. You can also use HttpRuntime.Cache. Little less overhead when
    using this one.

    I believe they are in System.Web
    -joe

    On May 30, 8:28 pm, "Jordan S." <> wrote:
    > "Peter Bucher [MVP]" <> wrote in messagenews:...
    >
    > > Hello Jordan S.

    >
    > >> How can I access the same Cache from *within* a custom composite Web
    > >> server control I'm writing (that lives in it's own assembly) that is
    > >> dynamically added to a page at runtime? What do I need to do with the
    > >> custom control or it's containing assembly to enable it to access the
    > >> Cache of the hosting Web application?

    > > Two Ways:

    >
    > > HttpContext.Current.Cache

    >
    > > this.Page.Cache

    >
    > > both gives the same Reference to the current Cache Object.

    >
    > Unfortunately neither of your suggestions works in my case. I want to access
    > the Cache from *within* a web server control. There is no "this.Page"
    > reference in a web server control.
    >
    > The suggestion for HttpContext.Current.Cache seems promising, but it's
    > currently not available in my control's class.
    >
    > Am I just missing some reference? The project/assembly within which my
    > control exists references System.Web. What else do I need to reference - or
    > what do I need to do to be able to access teh current context from within a
    > custom control?
    >
    > Thanks.
    Joe, May 31, 2008
    #4
  5. Jordan S.

    Jordan S. Guest

    Got it... I was referencing System.Web... but was missing the using
    statement.... (Doh!....)

    Thanks.


    "Joe" <> wrote in message
    news:...

    Your just missing a reference. HttpContext.Current.Cache is the same
    object. You can also use HttpRuntime.Cache. Little less overhead when
    using this one.

    I believe they are in System.Web
    -joe

    On May 30, 8:28 pm, "Jordan S." <> wrote:
    > "Peter Bucher [MVP]" <> wrote in
    > messagenews:...
    >
    > > Hello Jordan S.

    >
    > >> How can I access the same Cache from *within* a custom composite Web
    > >> server control I'm writing (that lives in it's own assembly) that is
    > >> dynamically added to a page at runtime? What do I need to do with the
    > >> custom control or it's containing assembly to enable it to access the
    > >> Cache of the hosting Web application?

    > > Two Ways:

    >
    > > HttpContext.Current.Cache

    >
    > > this.Page.Cache

    >
    > > both gives the same Reference to the current Cache Object.

    >
    > Unfortunately neither of your suggestions works in my case. I want to
    > access
    > the Cache from *within* a web server control. There is no "this.Page"
    > reference in a web server control.
    >
    > The suggestion for HttpContext.Current.Cache seems promising, but it's
    > currently not available in my control's class.
    >
    > Am I just missing some reference? The project/assembly within which my
    > control exists references System.Web. What else do I need to reference -
    > or
    > what do I need to do to be able to access teh current context from within
    > a
    > custom control?
    >
    > Thanks.
    Jordan S., May 31, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reny J Joseph Thuthikattu
    Replies:
    1
    Views:
    555
    =?Utf-8?B?UHJha2FzaC5ORVQ=?=
    Dec 30, 2004
  2. =?Utf-8?B?b25l?=
    Replies:
    1
    Views:
    5,283
    Karl Seguin [MVP]
    Mar 8, 2006
  3. John
    Replies:
    2
    Views:
    1,179
  4. Tim
    Replies:
    0
    Views:
    203
  5. Yaron Cohen
    Replies:
    0
    Views:
    326
    Yaron Cohen
    Nov 19, 2003
Loading...

Share This Page