Reputation: 18117
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 ?
Upvotes: 1
Views: 3382
Reputation: 5323
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.
Upvotes: 5
Reputation: 1502146
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.
Upvotes: 2