简体   繁体   English

我可以从ASP.NET应用程序外的ASP.NET页面对象中呈现html吗?

[英]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. 我不是在谈论使用'ApplicationHost'类托管ASP.NET。 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? 例如,如果我创建一个Console应用程序,创建一个有效的HttpContext对象并将其传递给自定义Page对象的ProcessRequest,它是否会填充HttpReponse html,就好像它在ASP.NET中运行一样?

I don't see why not. 我不明白为什么不。

Try the RenderControl() method to get the html from a page or Web control. 尝试使用RenderControl()方法从页面或Web控件获取html。

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. 我使用它来异步渲染GridViews。

If you're talking custom ASP.NET controls, then you can programmatically create them and get them to render to a string easily enough. 如果您正在讨论自定义ASP.NET控件,那么您可以以编程方式创建它们并让它们足够容易地呈现为字符串。 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. 如果那是你有兴趣做的事情,那么我过去就已经做过了,可以为你挖出代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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