Issue with HttpContext and HttpHandlers with Server.Transfer

Discussion in 'ASP .Net' started by Steve Lutz, Jun 21, 2005.

  1. Steve Lutz

    Steve Lutz Guest

    Hello All,

    I have an ASPX page whose class inherits from a company global base page.

    The company base page has a property call PageTitle (string) that is
    assigned by all the pages.
    The base class also includes a class that is used for logging. This logging
    class uses HttpContext.Current.Handler to get the instance of the page. In
    this way, it can get the PageTitle property.

    This works just fine with normal .ASPX pages, however, I also have an
    HttpHandler that handles special URL's. This handler determines the page
    that should handle the request, and then does a server.transfer to the
    correct page.

    The handler works, however, upon instantiation of the logging class of the
    base page, it gets an error because the HttpContent.Current.Handler says
    that it's the HttpHandler, and not the basepage.

    Is there anyway I can get the instance of the Page's class, instead of the
    Handler's class after using a HttpHandler and a Server.Transfer?

    Thanks

    Steve
    Steve Lutz, Jun 21, 2005
    #1
    1. Advertising

  2. Steve Lutz

    Scott Allen Guest

    Hi Steve:

    I'm a little confused -

    You have a class derived from System.Web.UI.Page that serves as a base
    class for all your pages. The base class includes the PageTitle
    property and some logging code. Why does the class use
    HttpContext.Current.Handler to get a reference to itself? Is the
    method static / shared?

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Mon, 20 Jun 2005 21:53:04 -0400, "Steve Lutz"
    <> wrote:

    >Hello All,
    >
    >I have an ASPX page whose class inherits from a company global base page.
    >
    >The company base page has a property call PageTitle (string) that is
    >assigned by all the pages.
    >The base class also includes a class that is used for logging. This logging
    >class uses HttpContext.Current.Handler to get the instance of the page. In
    >this way, it can get the PageTitle property.
    >
    >This works just fine with normal .ASPX pages, however, I also have an
    >HttpHandler that handles special URL's. This handler determines the page
    >that should handle the request, and then does a server.transfer to the
    >correct page.
    >
    >The handler works, however, upon instantiation of the logging class of the
    >base page, it gets an error because the HttpContent.Current.Handler says
    >that it's the HttpHandler, and not the basepage.
    >
    >Is there anyway I can get the instance of the Page's class, instead of the
    >Handler's class after using a HttpHandler and a Server.Transfer?
    >
    >Thanks
    >
    >Steve
    >
    Scott Allen, Jun 21, 2005
    #2
    1. Advertising

  3. Steve Lutz

    Steve Lutz Guest

    Hi Scott,

    You were on the right track, but you fell off a little - sorry about the
    poor explaination, here's a better one:

    The Master baseclass contains a property called PageTitle. (We'll call this
    class MasterPage)

    There is another class called PageContext which is used to generate
    Analyitic information. An instance of the PageContext class is created in
    MasterPage's
    contstructor. PageContext requires PageTitle from the masterPage to do it's
    work. So to get this:

    PageContext uses the current HttpContext to get the instance of the page
    that it's on (HttpContext.Current.Handler) which is then cast into a
    MasterPage, in order to get the PageTitle.

    The problem is that with an HttpHandler, the HttpContext.Current.Handler is
    not the MasterPage, even after a Server.Transfer to a page that does inherit
    from MasterPage.

    So the question is, is it possible to get the instance of the actual page
    that is currently executing? I know it exists, because an instance
    PageContext exists and that is created by the MasterPage.

    I do have some-workaround changes (like sticking PageContext into an
    HttpContext.Item, or creating an HttpHandler factory instead of using a
    true HttpHandler).

    Thanks for looking at this for me.

    Steve



    "Scott Allen" <> wrote in message
    news:...
    > Hi Steve:
    >
    > I'm a little confused -
    >
    > You have a class derived from System.Web.UI.Page that serves as a base
    > class for all your pages. The base class includes the PageTitle
    > property and some logging code. Why does the class use
    > HttpContext.Current.Handler to get a reference to itself? Is the
    > method static / shared?
    >
    > --
    > Scott
    > http://www.OdeToCode.com/blogs/scott/
    >
    > On Mon, 20 Jun 2005 21:53:04 -0400, "Steve Lutz"
    > <> wrote:
    >
    >>Hello All,
    >>
    >>I have an ASPX page whose class inherits from a company global base page.
    >>
    >>The company base page has a property call PageTitle (string) that is
    >>assigned by all the pages.
    >>The base class also includes a class that is used for logging. This
    >>logging
    >>class uses HttpContext.Current.Handler to get the instance of the page. In
    >>this way, it can get the PageTitle property.
    >>
    >>This works just fine with normal .ASPX pages, however, I also have an
    >>HttpHandler that handles special URL's. This handler determines the page
    >>that should handle the request, and then does a server.transfer to the
    >>correct page.
    >>
    >>The handler works, however, upon instantiation of the logging class of the
    >>base page, it gets an error because the HttpContent.Current.Handler says
    >>that it's the HttpHandler, and not the basepage.
    >>
    >>Is there anyway I can get the instance of the Page's class, instead of the
    >>Handler's class after using a HttpHandler and a Server.Transfer?
    >>
    >>Thanks
    >>
    >>Steve
    >>

    >
    Steve Lutz, Jun 22, 2005
    #3
  4. Steve Lutz

    Scott Allen Guest

    Oh! Makes sense now :)

    I'm trying this out and do not see a way, offhand. In fact my Handler
    property is null after the transfer, which is really odd. Are you
    doing the Server.Transfer during the BeginRequest event handler for
    your module? That's where mine is.

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/


    On Tue, 21 Jun 2005 21:03:15 -0400, "Steve Lutz"
    <> wrote:

    >Hi Scott,
    >
    >You were on the right track, but you fell off a little - sorry about the
    >poor explaination, here's a better one:
    >
    >The Master baseclass contains a property called PageTitle. (We'll call this
    >class MasterPage)
    >
    >There is another class called PageContext which is used to generate
    >Analyitic information. An instance of the PageContext class is created in
    >MasterPage's
    >contstructor. PageContext requires PageTitle from the masterPage to do it's
    >work. So to get this:
    >
    >PageContext uses the current HttpContext to get the instance of the page
    >that it's on (HttpContext.Current.Handler) which is then cast into a
    >MasterPage, in order to get the PageTitle.
    >
    >The problem is that with an HttpHandler, the HttpContext.Current.Handler is
    >not the MasterPage, even after a Server.Transfer to a page that does inherit
    >from MasterPage.
    >
    >So the question is, is it possible to get the instance of the actual page
    >that is currently executing? I know it exists, because an instance
    >PageContext exists and that is created by the MasterPage.
    >
    >I do have some-workaround changes (like sticking PageContext into an
    >HttpContext.Item, or creating an HttpHandler factory instead of using a
    >true HttpHandler).
    >
    >Thanks for looking at this for me.
    >
    >Steve
    >
    >
    >
    >"Scott Allen" <> wrote in message
    >news:...
    >> Hi Steve:
    >>
    >> I'm a little confused -
    >>
    >> You have a class derived from System.Web.UI.Page that serves as a base
    >> class for all your pages. The base class includes the PageTitle
    >> property and some logging code. Why does the class use
    >> HttpContext.Current.Handler to get a reference to itself? Is the
    >> method static / shared?
    >>
    >> --
    >> Scott
    >> http://www.OdeToCode.com/blogs/scott/
    >>
    >> On Mon, 20 Jun 2005 21:53:04 -0400, "Steve Lutz"
    >> <> wrote:
    >>
    >>>Hello All,
    >>>
    >>>I have an ASPX page whose class inherits from a company global base page.
    >>>
    >>>The company base page has a property call PageTitle (string) that is
    >>>assigned by all the pages.
    >>>The base class also includes a class that is used for logging. This
    >>>logging
    >>>class uses HttpContext.Current.Handler to get the instance of the page. In
    >>>this way, it can get the PageTitle property.
    >>>
    >>>This works just fine with normal .ASPX pages, however, I also have an
    >>>HttpHandler that handles special URL's. This handler determines the page
    >>>that should handle the request, and then does a server.transfer to the
    >>>correct page.
    >>>
    >>>The handler works, however, upon instantiation of the logging class of the
    >>>base page, it gets an error because the HttpContent.Current.Handler says
    >>>that it's the HttpHandler, and not the basepage.
    >>>
    >>>Is there anyway I can get the instance of the Page's class, instead of the
    >>>Handler's class after using a HttpHandler and a Server.Transfer?
    >>>
    >>>Thanks
    >>>
    >>>Steve
    >>>

    >>

    >
    Scott Allen, Jun 22, 2005
    #4
  5. Steve Lutz

    csmartdalton

    Joined:
    Jul 6, 2009
    Messages:
    1
    Solution

    For anybody who might read this, a good solution is to use

    Code:
    HttpContent.Current.[I]CurrentHandler[/I]
    instead of

    Code:
    HttpContent.Current.[I]Handler[/I]
    csmartdalton, Jul 6, 2009
    #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. =?Utf-8?B?YWxiZXJ0b3Nvcmlh?=

    Server.Transfer fails because httpcontext.current = nothing

    =?Utf-8?B?YWxiZXJ0b3Nvcmlh?=, Oct 24, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    1,315
    =?Utf-8?B?YWxiZXJ0b3Nvcmlh?=
    Oct 25, 2007
  2. Replies:
    1
    Views:
    459
    George Ter-Saakov
    Jan 11, 2008
  3. Fernando Rodriguez

    HttpHandlers, Server.Transfer and Session State

    Fernando Rodriguez, Apr 27, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    464
    news.microsoft.com
    Apr 28, 2008
  4. Tony

    Dim context As HttpContext = HttpContext.Current

    Tony, Mar 2, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    306
    Teemu Keiski
    Mar 3, 2004
  5. Jim
    Replies:
    1
    Views:
    873
    Aaron Bertrand - MVP
    Jan 28, 2004
Loading...

Share This Page