与Asp.net异步的Response.Redirect问题

new*_*man 8 c# asp.net response.redirect async-await

我是asp.net 4.5异步的新手,并且在异步方法中调用response.redirect会遇到以下情况.问题是响应只是"挂起"有没有其他人在尝试使用异步重定向时遇到过类似的问题?此代码将在一个全新的项目中使用,但不能与我们现有代码中的新页面一起使用.我确保从web.config中删除所有内容并删除了我们的母版页.打砖墙......任何想法?谢谢!

    protected void Page_Load(object sender, EventArgs e)
    {
        RegisterAsyncTask(new PageAsyncTask(PageLoadAsync));
    }

    private async Task PageLoadAsync()
    {
        var data = await GetData();

        if (data == HttpStatusCode.OK)
            Response.Redirect("http://www.google.com");
    }

    private async Task<HttpStatusCode> GetData()
    {
        using (var client = new HttpClient())
        {
            var response = await client.GetAsync("https://www.google.com");
            return response.StatusCode;
        }
    }
Run Code Online (Sandbox Code Playgroud)

Ste*_*ary 17

此代码将在一个全新的项目中使用,但不能与我们现有代码中的新页面一起使用.

我假设您现有的网站已经升级到.NET 4.5.

首先要检查的httpRuntime.targetFramework是设置为4.5.升级时,默认情况下不会设置此项.

从评论中编辑:

要检查的另一件事(以防万一)Page.Async是设置为true.

在这种情况下,溶液是调用Response.Redirect("http://www.google.com", false),其中明确传递falseendResponse参数.默认值true仅用于此处所述的向后兼容性原因.