How to 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 use this function to load contents from different files, but "page.ProcessRequest (HttpContext.Current)" inserts the contents into the current context, and I need a function that returns the contents of the specified file.

I wonder if there is a way to create a new HttpContext so that "page.ProcessRequest" does not contribute anything to the current response.

+5
source share
4 answers

Oded is correct, as far as I know. You cannot easily create your own instance of HttpContext. However, you can still achieve your goals in other ways.

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

HttpHandler TextWriter, .

+3

HttpContext, .

ASP.NET BCL - - (, , HttpContext.

what I need is the function to return the content of the specified file - , ?

+1

Pex/Moles , , . ( )

+1
public class HttpContextManager
        {
            private static HttpContextBase _context;
            public static HttpContextBase Current
            {
                get
                {
                    if (_context != null)
                        return _context;

                    if (HttpContext.Current == null)
                        throw new InvalidOperationException("HttpContext not available");

                    return new HttpContextWrapper(HttpContext.Current);
                }
            }

            public static void SetCurrentContext(HttpContextBase context)
            {
                _context = context;
            }

        }
-1
source

All Articles