ASP.Net:在Page

编程入门 行业动态 更新时间:2024-10-28 14:26:46
本文介绍了ASP.Net:在Page_Load中调用异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个API客户端,该客户端具有一个send方法,该方法用于将我的对象发布到Web API服务并返回ReturnedResults类型的对象. 现在,我需要在ASP page_load事件中运行此post方法. 我在这里看到过类似的示例,但是我需要从异步方法中获取返回对象.

I have this API client which has a send method used to post my object to a Web API service and return an object of type ReturnedResults. Now I need to run this post method in an ASP page_load event. I have seen similar examples here, however what I need is to get my return object from my async method.

我知道我应该使用

PageAsyncTask t = new PageAsyncTask(APIService.Send("test"));

但是,我有两个问题,第一个是PageAsyncTask不接受我的Send方法作为有效的Task类型,我想这是因为我的方法返回Task类型的Task,所以它抱怨无法将Task转换为System.Func.

However I have two problems, first PageAsyncTask doesn't accept my Send method as a valid Task type, I guess that's because my method returns a Task of Task type so it complains that it can't convert Task to System.Func

成功执行后,如何从其中获取ReturnedResults对象?

Also how can I get my object of ReturnedResults out of this once it is successfully executed?

推荐答案

仅仅因为您不能等待PageAsyncTask的结果,并不意味着任务本身无法启动更多您可以使用的功能等待.您可以利用它来加载异步页面.

Just because you can't await the result of a PageAsyncTask doesn't mean that the task itself can't kick off more functions that you can then await. You can take advantage of this to have an async page load.

public void Page_Load(object sender, EventArgs e) { RegisterAsyncTask(new PageAsyncTask(PageLoadAsync)); } public async Task PageLoadAsync() { //perform your actions here, including calling async methods and awaiting their results Task<string> downloadTask = new WebClient().DownloadStringTaskAsync("www.example"); TextBox1.Title = "We can do some actions while waiting for the task to complete"; TextBox2.Title = await downloadTask; }

确保您的页面被标记为异步.

Make sure your page is marked as async.

<%@ Page Language="C#" CodeBehind="Default.aspx.cs" Inherits="Default" Async="true" %>

更多推荐

ASP.Net:在Page

本文发布于:2023-11-10 15:31:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575760.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ASP   Net   Page

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!