如何在 asp.net 中的单个回发中发送响应一次后使用 response.redirect 发送新响应
我使用response.write和response.flush在页面初始化上显示加载div 直到页面完全呈现的时候。 像这样:
Page.Response.Write(loadinghtmlstring);
Page.Response.Flush();
如果通过 try catch 发生某些错误,我将重定向到错误页面。
try
{}
catch (WrapedException wre)
{
BaseUserControl.StoreException = wre;
Response.Redirect(base.ErrorPagePath, false);
}
catch (Exception ex)
{
BaseUserControl.StoreException = new WrapedException(ex.Message, ex);
Response.Redirect(base.ErrorPagePath, false);
}
现在,如果在显示加载 div 后我的页面出现任何错误 我收到错误消息:
无法在 HTTP 标头之后重定向 已发送。
I am displaying a loading div on the page init using response.write and response.flush
until the time page is fully rendered .
like this:
Page.Response.Write(loadinghtmlstring);
Page.Response.Flush();
I am redirecting to error page if some error occurs through try catch.
try
{}
catch (WrapedException wre)
{
BaseUserControl.StoreException = wre;
Response.Redirect(base.ErrorPagePath, false);
}
catch (Exception ex)
{
BaseUserControl.StoreException = new WrapedException(ex.Message, ex);
Response.Redirect(base.ErrorPagePath, false);
}
Now if any error occurs on my page after displaying loading div
I got error which says :
Cannot redirect after HTTP headers
have been sent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这可能不完全是您在答案中寻找的内容...但是您为什么不使用 Javascript 作为加载器对话框呢? YUI 有一个很棒的加载对话框这里,我确信 jquery也有一个。这会降低页面上 ASP.NET 生成加载程序的复杂性,并且可能会解决您的错误问题。
I know this may not be EXACTLY what your looking for in an answer... but why don't you use Javascript for your loader dialog? YUI has a great loading dialog here and I am sure jquery also has one as well. THis would take the complexity of having an ASP.NET generated loader on the page, and may solve your error problems.
发生这种情况是因为您正在写入并刷新响应(通过刷新写入的标头)。重定向需要覆盖这些标头,但目前无法覆盖。一旦调用了 Response.Flush(),就无法使用 Response.Redirect()。
您可以执行以下操作:
This is happening because you are writing and flushing the response (by flushing the headers get written). Redirect needs to overwrite these headers and cannot at this point. Once you have called
Response.Flush()
, there is no way to use aResponse.Redirect()
.You can do the following: