Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 429
Carlos Avatar asked Aug 28 '11 19:08

Carlos


2 Answers

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.

like image 167
Kenneth Ito Avatar answered Oct 01 '22 01:10

Kenneth Ito


You can't create a new HttpContext, not without lots of work arounds.

It is one of the failings of ASP.NET and the BCL - makes web applications untestable (or at least very difficult to test without HttpContext.

I am not clear on your requirement what I need is the function to return the content of the specified file - can you please explain exactly what you mean by that?

like image 32
Oded Avatar answered Oct 01 '22 03:10

Oded