Create page as HttpHandler with Pagepaser

Source: Internet
Author: User
Keywords Http dler as
Tags .net asp asp.net aspx class code compiler content

In ASP.net, page is actually a httphandler, which processes the request, and then returns a whole bunch of HTML. So is it possible to instantiate a page class and get the results of its processing? A simple reasoning can explain that this is not possible. Because if we instantiate page, so the new page (), this is a standard. NET code, the compiler is not aware of the relevant page.aspx content. So you need Pagepaser help, and Pagepaser is also a key step in asp.net internal compilation process. Here's how to use it:

protected void Page_Load (object sender, EventArgs e)
{
Response.Write (Httputility.htmlencode (Dumphtmlfrom ("~/default.aspx"));
}

Virtualpath:allow queries
String Dumphtmlfrom (String virtualpath)
{
StringWriter writer = new StringWriter ();
var path = Request.Url.Scheme + "://" + Request.Url.Authority + virtualpathutility.toabsolute (virtualpath);
var parts = virtualpath.split ('? ');

string query = String. Empty;
if (parts. Length > 1)
query = Parts[1];
VirtualPath = Parts[0];

HttpContext context = new HttpContext (New HttpRequest (virtualpath, path, query), new HttpResponse (writer));
var handler = pageparser.getcompiledpageinstance (VirtualPath, MapPath (virtualpath), context);
Handler. ProcessRequest (context);

return writer. ToString ();
}

One of the key steps Pageparser.getcompiledpageinstance instantiate a HttpHandler and return all the HTML.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.