[英]How to prerender a UserControl with async/await on server-side for ASP.net WebForms
I have to deal with a WebForms (.Net 4.7) application, that loads server-side prerendered UserControls, by calling a HttpHandler by Javascript/Angular from client-side. 我必须处理一个WebForms(.Net 4.7)应用程序,该应用程序通过从客户端通过Javascript / Angular调用HttpHandler来加载服务器端预渲染的UserControl。
The HttpHandler prerenders the UserControls by this old "hack" HttpHandler通过这个旧的“ hack”来呈现UserControls
public class MyHandler : IHttpHandler
{
public override void ProcessRequest(HttpContext context)
{
var page = new Page();
var control = page.LoadControl("~/MyFancyUC.ascx");
var form = new HtmlForm { ID = "Form1" };
form.Controls.Add(control);
page.Controls.Add(form);
var stringBuilder = new StringBuilder();
using (StringWriter output = new StringWriter(stringBuilder))
{
HttpContext.Current.Server.Execute(page, output, false);
}
context.Response.Write(stringBuilder.ToString());
}
}
As this style worked several years very well, I can't use async/await style within the UserControls, because I'm getting an error, that the Page needs to have the Async=true attribute. 由于这种样式已经运行了好几年,因此我无法在UserControls中使用async / await样式,因为出现错误,页面需要具有Async = true属性。
The UserControls look like this: UserControls看起来像这样:
public partial class MyFancyUC: UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Page.RegisterAsyncTask(new PageAsyncTask(HandleAsync));
}
private async Task HandleAsync()
{
var data = await GetDataAsync();
BindData(data);
}
}
I already tried to create a simple page class, that sets the Async mode programmatically, and use it in the "MyHandler" HttpHandler, but this doesn't work either. 我已经尝试创建一个简单的页面类,该页面类以编程方式设置异步模式,并在“ MyHandler” HttpHandler中使用它,但这也不起作用。
public class AsyncPage : Page
{
public AsyncPage()
{
this.AsyncMode = true;
}
}
} }
When I debug the UserControl, the Page property always shows AsyncMode=false, IsAsync = false. 当我调试UserControl时,Page属性始终显示AsyncMode = false,IsAsync = false。
So I guess, this is caused by HttpServerUtility.Execute() call, that is not capable of running a WebForms page in Async mode. 所以我想这是由HttpServerUtility.Execute()调用引起的,该调用无法在异步模式下运行WebForms页面。
Does anyone know how to deal with this ? 有人知道如何处理吗?
An asynchronous HTTP handler must implement IHttpAsyncHandler
Interface . 异步HTTP处理程序必须实现IHttpAsyncHandler
接口 。
An asynchronous Page
must have its AsyncMode
property set to true
. 异步Page
必须将其AsyncMode
属性设置为true
。
您必须使用IHttpAsyncHandler
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.