Response.Redirect的问题与Asp.net异步 [英] Response.Redirect issue with Asp.net async
问题描述
我是新来的ASP.NET 4.5异步和正在运行到以下与异步方法中调用的Response.Redirect。问题是,响应只是挂有其他人遇到类似的问题与试图与异步的重定向?这code将在一个全新的项目工作,但是,随着新的一页在我们现有的code不起作用。我确信肠道出尽我所能我们的web.config并取消我们的母版页。击中砖墙...任何想法?谢谢!
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
RegisterAsyncTask(新PageAsyncTask(PageLoadAsync));
} 私人异步任务PageLoadAsync()
{
VAR数据=等待的GetData(); 如果(数据==的HTTPStatus code.OK)
的Response.Redirect(http://www.google.com);
} 私人异步任务<的HTTPStatus code>的GetData()
{
使用(VAR的客户=新的HttpClient())
{
VAR响应=等待client.GetAsync(https://www.google.com);
返回response.Status code;
}
}
这code将在一个全新的项目工作,但是,不以新的一页在我们现有的code工作。
块引用>我假设你现有的网站已经被升级到.NET 4.5。
要检查的第一件事是,
httpRuntime.targetFramework
设置为4.5
。这是的不的通过升级时默认设置。从评论编辑:
另一件事来检查(以防万一)是
Page.Async
设置为真正
。在这种情况下,解决办法是打电话
的Response.Redirect(http://www.google.com,FALSE)
,其中明确通过假
为endResponse
参数。的真正
仅用于向后兼容的原因<一个默认值href=\"http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-htt$p$psponse-redirect.aspx\">as这里描述。I'm new to asp.net 4.5 async and am running into the following with calling response.redirect within an async method. The issue is that the response just "hangs" Has anyone else experienced similar issues with attempting an redirect with async? This code will work in a brand new project, but, does not work with a new page in our existing code. I made sure to gut out everything I could out of our web.config and removed our master page. Hitting a brick wall...any ideas? Thanks!
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; } }
解决方案This code will work in a brand new project, but, does not work with a new page in our existing code.
I assume your existing site has already been upgraded to .NET 4.5.
The first thing to check is that
httpRuntime.targetFramework
is set to4.5
. This is not set by default when you upgrade.Edit from comments:
Another thing to check (just in case) is that
Page.Async
is set totrue
.In this case, the solution was to call
Response.Redirect("http://www.google.com", false)
, which explicitly passesfalse
for theendResponse
parameter. The default value oftrue
is only for backwards-compatibility reasons as described here.这篇关于Response.Redirect的问题与Asp.net异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!