プログラムで aspx ページを文字列にレンダリングしようとしています。レンダリングは、コンソール アプリから実行されます。今のところ、aspx ページでプログラムによって宣言されたコントロールで動作するようになりました。これはすべて問題なくダンディです。しかし、静的に宣言されたコントロールはどうでしょうか? それを達成する方法はありますか?
コード例: ページのレンダリング:
static string RenderPage() {
ClassLibrary1.FormlessPage pageHolder = (ClassLibrary1.FormlessPage)new WebApplication1.WebForm1();
pageHolder.DesignerInitialize();
StringWriter output = new StringWriter();
HttpRequest httpRequest = new HttpRequest("", "http://localhost/", "");
HttpResponse httpResponse = new HttpResponse(output);
HttpContext context = new HttpContext(httpRequest, httpResponse);
context.Server.Execute(pageHolder, output, false);
return output.ToString();
}
フォームレス ページ クラス
namespace ClassLibrary1 {
public class FormlessPage : System.Web.UI.Page {
public override void VerifyRenderingInServerForm(Control control) {}
}
}
ここで、WebForm1 (参照 Web アプリケーション プロジェクトにありますが、同じプロジェクト内で行われた場合の動作は同じです) でコントロールを宣言すると、正常にレンダリングされます。
public partial class WebForm1 : ClassLibrary1.FormlessPage {
protected void Page_Load(object sender, EventArgs e) {
this.Controls.Add(new TextBox() { ID = "tbSomeText", Text = "default text" });
}
}
...しかし、静的 html および静的に宣言されたコントロールはレンダリングされません。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
...blah...blah...
<form id="form1" runat="server">
<div>
text
<asp:TextBox runat="server" ID="tbSomething"></asp:TextBox>
</div>
</form>
...blah...blah...
この動作を完全に偽造し、マークアップのレンダリングも実現する方法はありますか? 上記のレンダリングされた出力は、次のとおりです。
<input name="tbSomeText" type="text" value="default text" id="tbSomeText" />