Reputation: 227
Given an instance of a System.Web.UI.Page, is there any way to get the current HttpContext from it? There is a protected Context property, but I'm hoping for something exposed publicly.
Upvotes: 0
Views: 1048
Reputation: 155598
You can also get it via RequestContext
which is a public property. Though it returns HttpContextBase
instead of HttpContext
but that should be fine for most applications.
public static HttpContextBase GetHttpContext( this System.Web.UI.Page page )
{
if( page == null ) throw new ArgumentNullException(nameof(page));
return page.Request.RequestContext.HttpContext;
}
As it's an extension method, you only need to do:
Page page = ... // Get `page` instance from somewhere.
page.GetHttpContext().Response.WriteLine("working!");
Upvotes: 0
Reputation: 15673
you can access the current context from anywhere with
HttpContext.Current
Upvotes: 3