ana*_*nay 1 .net c# asp.net response.redirect exception
我想在发生异常时使用Response.Redirect重定向浏览器.
我还想将异常消息传递给我的错误页面.
例如:
string URL = "Page2.aspx?Exception=" + ex.ToString()
Response.Redirect(URL)
Run Code Online (Sandbox Code Playgroud)
可以吗?这是正确的语法吗?
而不是Response.Redirect,它向客户端发送响应,要求它请求不同的页面,您应该调用Server.Transfer,它立即运行不同的页面并将该页面直接发送到客户端.
然后,您可以将异常放入错误页面HttpContext.Items并从中读取HttpContext.Items.
例如:
catch (Exception ex) {
HttpContext.Current.Items.Add("Exception", ex);
Server.Transfer("Error.aspx");
}
Run Code Online (Sandbox Code Playgroud)
在Error.aspx,你可以得到这样的例外:
<%
Exception error;
if (!HttpContext.Current.Items.Contains("Exception"))
Response.Redirect("/"); //There was no error; the user typed Error.aspx into the browser
error = (Exception)HttpContext.Current.Items["Exception"];
%>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9396 次 |
| 最近记录: |