Search code examples
c#.netasp.nethttpcontext

How can I create a new HttpContext?


public void getContent() {
    string VirtualPath = "~/Content.aspx";
    var page = BuildManager.CreateInstanceFromVirtualPath( VirtualPath, typeof( Page ) ) as IHttpHandler;
    page.ProcessRequest( HttpContext.Current );
}

I'm using that function to load the content from different files, but the "page.ProcessRequest( HttpContext.Current )" inserts the content at the current context, and what I need is the function to return the content of the specified file.

I wonder if there's a working way to create a new HttpContext, so that "page.ProcessRequest" don't insert anything into the current response.


Solution

  • Oded is correct as far as I know. You can't easily create your own instance of the HttpContext. However you can still achieve your goals thorugh other means.

    Use a Server.Execute. http://msdn.microsoft.com/en-us/library/ms150027.aspx.

    You can specify the HttpHandler to execute along with a TextWriter to dump the content into.