1

Within some library class, is there a way I can I determine which webpage called it, without having to pass in the calling Page object ?

Tomas
  • 15,501
  • 38
  • 136
  • 233
  • Well, if the information resides in the Page object, then how do you think you can get that information *without* referring to the Page object? – Stephen Chung Apr 07 '11 at 06:40

2 Answers2

5

See the answer given here: Get current System.Web.UI.Page from HttpContext?

You're looking for HttpContext.Handler. Since Page implements IHttpHandler, you'll obtain a reference to the currently executing page.You'll have to cast it, or at least try to cast it to the particular type you're looking for.

Community
  • 1
  • 1
Arjen
  • 4,183
  • 3
  • 15
  • 19
2

Well, you could use HttpContext.Current to get the current HttpContext, which lets you find out the request. I don't believe that will give you access to the Page itself though 1... if you really need the actual Page, I think passing it into the method would be the cleanest approach. (It removes some of the "magic" of thread-locals, and also makes it easier to test the library code.)

What does your library code need to do with the page? Is the library strongly tied to web applications, or might it be useful in other contexts?


1 I see from another answer that the Page itself is the Handler, so you'd just need a cast. This still feels somewhat ugly to me though.

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929