C# 此时无法启动异步操作。

标签 c# asp.net asynchronous

当我单击以 asp web 表单提交的按钮时,我尝试运行我的代码。我一点击按钮就出现错误有人知道原因吗?错误消息显示如下:

Server Error in '/' Application.

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>.

aspx 文件:

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="jumbotron">
    <h1>Luis</h1>        
        <asp:TextBox ID="TextBox1" runat="server" Height="40px" Width="273px"></asp:TextBox>    
</div>
    <p>
       <input id="Submit1"  runat="server" type="submit" value="Check" onserverclick="Submit_Click"/></p>  
    <p>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</p>
</asp:Content>

.cs 文件:

public partial class _Default : Page
{
public async void Submit1_Click(object sender, EventArgs e) {

    string utterance = TextBox1.Text;

    var client = new HttpClient();
    var queryString = HttpUtility.ParseQueryString(string.Empty);

    // This app ID is for a public sample app that recognizes requests to turn on and turn off lights
    var luisAppId = "75bcaaff-1dc1-4dsa-adf7-63584cea339a";
    var subscriptionKey = "43314f19c5ecgdascba6a00f1d3cc3533";

    // The request header contains your subscription key
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

    // The "q" parameter contains the utterance to send to LUIS
    //queryString["q"] = "turn on the left light";

    // These optional request parameters are set to their default values
    queryString["timezoneOffset"] = "0";
    queryString["verbose"] = "false";
    queryString["spellCheck"] = "false";
    queryString["staging"] = "false";

    var uri = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/" + luisAppId + "?" + queryString + "&q=" + utterance;
    var response = await client.GetAsync(uri);

    var strResponseContent = await response.Content.ReadAsStringAsync();

    // Display the JSON result from LUIS
    //Console.WriteLine(strResponseContent.ToString());
    Label1.Text = strResponseContent.ToString();
}
}

最佳答案

您正在调用 ASYNC 方法,答案就在错误中。

ensure that the Page is marked <%@ Page Async="true" %>.

关于C# 此时无法启动异步操作。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51682205/

相关文章:

c# - 要提供哪种参数类型(即 IEnumerable 且具有 Count)?

c# - 如何在详细 View 中为命令字段添加删除确认提示?

node.js - NodeJS : Async/Await Raspberry Pi

asp.net - 使用 ndepend 分析 asp.net 站点

iphone - 弹出 UIViewController 时处理打开的异步 Web 请求 (AFNetworking)

javascript - 如何在等待多个异步 JSONP 回调时显示加载消息?

c# - 尝试使用 linq 转换数据

c# - WebAPI 不发送数据

c# - Textmate 中最常用的快捷键不起作用?

c# - 从选定的 DropDownList 值中仅删除最后一个逗号