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

时间:2016-09-07 10:15:01

标签: c# asp.net azure-web-sites

我试图在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
    }

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:-1)

您可以编写this.AsyncMode = true;页面加载不足以允许执行异步任务。