此操作要求页面是异步的(Async属性必须设置为true)

本文关键字:属性 Async 设置 true 操作 异步 | 更新日期: 2023-09-27 18:06:05

我试图在Page_Load方法内执行Azure托管网页上的异步任务。然而,我得到了上面的错误。我在aspx文件中将页面的Async属性设置为true,但仍然没有运气。

ASP标头代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SalesAndCCFAQ.FAQ" Async ="true"%>

Page_Load代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        RegisterAsyncTask(new PageAsyncTask(test));
        Page.ExecuteRegisteredAsyncTasks();
        //Only need to fill catDropDown when the page is first directed to
        if (!Page.IsPostBack)
        {
            fillCatDropDown();
            //Show who is currently logged in
            currentUserLabel.Text += HttpContext.Current.User.Identity.Name;           
        }
        if (GlobalUse.external == true)
            {
                whichApproved = "approvedExternal = 1";
                greenKeyImage.Visible = false;
                greenKeyLabel.Visible = false;
                yellowKeyImage.Visible = false;
                yellowKeyLabel.Visible = false;
                redKeyImage.Visible = false;
                redKeyLabel.Visible = false;
            }
            else
            {
                whichApproved = "approvedInternal = 1";
            }
        //String to be added to main query if a category is selected
        filterCatQuery = "cid = (SELECT cid FROM Category WHERE name = '" + catDropDown.Text + "')";
    }

Page_PreInit代码:

    protected void Page_PreInit()
    {
        if (!Page.IsPostBack)
        {
            if (GlobalUse.external == true)
            {
                this.MasterPageFile = "~/SiteExternal.Master";
            }
        }
    }

调用的异步函数:

    protected async Task test()
    {
        if (GlobalUse.external != null)
            return;
        await GlobalUse.isUserExternalGroup(); //This method sets GlobalUse.external
        Response.Redirect("~/Default.aspx"); //Refresh to call the PreInit Code again
    }

此操作要求页面是异步的(Async属性必须设置为true)

将Async设置为true,如下所示。

<%@ Page Title="" Language="C#" Async="true" MasterPageFile="~/XXX.Master" AutoEventWireup="true"
CodeBehind="XXX.aspx.cs" Inherits="XXX"
ValidateRequest="false" meta:resourcekey="XXX" %>     

抛出这个错误的原因是因为它是我的默认页面。我修复它的方式是通过创建一个空白的默认页面,然后在加载时重定向到我想要的主页。我将把它留在这里,因为它可能对某些人有用,我相信这是一个非常独特的案例。

你可以这样写。AsyncMode = true;