简体   繁体   English

如何在ASP.net WebForms的服务器端使用async / await预渲染UserControl

[英]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.

相关问题 服务器端变量的ASP.NET(UserControl)语法错误 - ASP.NET (UserControl) Syntax Error for server-side variable ASP.NET WebForms视图引擎无法解析服务器端标记 - ASP.NET WebForms view engine unable to parse server-side tag 如何使用ASP.NET MVC打印服务器端 - How to print server-side with asp.net mvc Asp.Net Webforms自定义UserControl中的事件 - Events in Asp.Net Webforms custom UserControl 在ASP.NET WebForms中在服务器端初始化bootsrap datatimepicker - Initializing bootsrap datatimepicker on server side in asp.net webforms 如何在服务器端捕获 ASP.NET Core 2 SignalR 异常并在客户端使用 JavaScript 处理它们? - How to catch ASP.NET Core 2 SignalR exceptions on server-side and handle them on client side with JavaScript? 如何在 ASP.NET Core 3.1 MVC 中进行RequiredIf 客户端和服务器端验证? - How to make RequiredIf Client-side and server-side validation in ASP.NET Core 3.1 MVC? 如何在ASP.NET中从客户端调用服务器端函数? - How to call server-side function from client-side in ASP.NET? ASP.NET双列表框使用JQuery更新了客户端,以及如何在服务器端检索结果 - ASP.NET dual listboxes updated client-side with JQuery, and how to retrieve the results server-side 如何从客户端调用服务器端ASP.NET事件 - how to call a server-side ASP.NET event from the client-side
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM