简体   繁体   中英

Can I render html from ASP.NET Page objects outside ASP.NET applications?

I'm not talking about hosting ASP.NET with the 'ApplicationHost' class. For example, if I create a Console application, create a valid HttpContext object and pass it to the ProcessRequest of a custom Page object, will it fill the HttpReponse html like if it was running inside ASP.NET?

I don't see why not.

Try the RenderControl() method to get the html from a page or Web control.

static public string GetHTML(Control myControl)
{
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter myWriter = new HtmlTextWriter(sw);
        myControl.RenderControl(myWriter);
        return sw.ToString();
}

I use this to render GridViews asynchronously.

If you're talking custom ASP.NET controls, then you can programmatically create them and get them to render to a string easily enough. If that's something you're interested in doing, then I've done it in the past and can dig the code out for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM