开发者

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 re开发者_Python百科turn 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.


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.


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?


Check out Pex/Moles its includes a mocking framework that can mock almost any type or member, even if its sealed or static. (it does this by using a custom test host)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜