如题。我在global.asax中进行异常处理,Application_Error中使用response.redirect时有时候会报错,有时候又不会报。
这是什么原因呢?global.asax: <%@ Application Language="C#" %><script runat="server"> void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码 }
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
Exception objErr = Server.GetLastError().GetBaseException();
Application["errorPage"] = Request.Url.ToString();
Application["errorMsg"] = objErr.Message;
Server.ClearError();
Response.Redirect("Error.aspx");//有时候会报异常:无法在发送 HTTP 标头之后进行重定向。
} void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
string str = DateTime.Now.ToString("yyyyMMdd");
int da = int.Parse(str);
if (da-20081113>=30)//如果试用期已到,转到错误页面,提示用户注册软件。
{
Application["zc"] = "0";
Response.Redirect("Error.aspx?id=4");
}
else
Application["zc"] = "1";
} void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。 }
</script>
这是什么原因呢?global.asax: <%@ Application Language="C#" %><script runat="server"> void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码 }
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
Exception objErr = Server.GetLastError().GetBaseException();
Application["errorPage"] = Request.Url.ToString();
Application["errorMsg"] = objErr.Message;
Server.ClearError();
Response.Redirect("Error.aspx");//有时候会报异常:无法在发送 HTTP 标头之后进行重定向。
} void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
string str = DateTime.Now.ToString("yyyyMMdd");
int da = int.Parse(str);
if (da-20081113>=30)//如果试用期已到,转到错误页面,提示用户注册软件。
{
Application["zc"] = "0";
Response.Redirect("Error.aspx?id=4");
}
else
Application["zc"] = "1";
} void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。 }
</script>
解决方案 »
- AJAX 传到服务器上出现sys未定义
- 在DataList中怎样显示一个字段的多个值
- 如何在XML里取一些数据然后在ASPX上画出来
- 编译器错误信息: CS0029: 无法将类型“int”隐式转换为“string”
- 如何把客户端的xml,schema提交发送到服务器端用dataset保存到数据库?
- 在ASP中怎么调用asp.net里的AdRotator控件
- 如何让DropDownList绑定数据库后,最上边显示自定义的内容?
- asp.net登录状态问题
- WebAPI通过HttpClient访问时如何指定返回格式
- 如何绑定datagrid中的dropdownlist?在线等
- 求助:将附件按年、月文件夹保存的思路
- 修改数据后,页面上显示的还是更新前的数据,重新登录后数据才发生变化?怎么回事? asp.net c#
lz给加分么?
如果在a.asp中,没有向客户端写入任何信息,此时就不会报错
如果在b.asp中,产生错误的时候已经向客户端写入了一些数据比如<html><header>....
此时将不能够用服务器端的自动跳转进行重定向。
解决的方法是,要么用客户端的跳转,比如js的跳转
要么修改b.asp,将写入的首先写入buffer,而非直接写到客户端
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码 try
{
Exception objErr = Server.GetLastError().GetBaseException();
Application["errorPage"] = Request.Url.ToString();
Application["errorMsg"] = objErr.Message;
Server.ClearError();
Response.Write("<script>this.window.location=\"error.aspx\";</script>");
//Response.Redirect("Error.aspx",true);//有时候会报异常:无法在发送 HTTP 标头之后进行重定向。
}
catch (Exception e2)
{
throw e2;
}
}
Response.Redirect("~/Error.aspx");//有时候会报异常:无法在发送 HTTP 标头之后进行重定向。
试试
会报错,好像字符串中不能出现“"</script>"”,为什么啊?
//这两句执行的时候有什么区别?第一句是找当前目录下的的Error.aspx,第二句是找当前网站根目录下的Error.aspx,都是同一个文件,其实是一样的吧?
Response.Redirect("Error.aspx");
Response.Redirect("~/Error.aspx");//
或者
执行 Response.Write("") 再执行 Response.Redirect
{
/*
if (Application["zc"] != null)
{
if (Application["zc"].ToString() == "0")
{
Response.Flush();
Response.Redirect("Error.aspx?id=4");
}
}
*/
if (Session["info"] != null)
Session["info"] = "";
//检查登录状态,如果用户已经登录,给出提示信息
if (Session["LogState"] != null)
if (Session["LogState"].ToString() == "1")
Response.Redirect("error.aspx?id=2");
txtUser.Focus();
}
修改global.asax为:<%@ Application Language="C#" %><script runat="server"> void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码 }
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
Exception objErr = Server.GetLastError().GetBaseException();
Application["errorPage"] = Request.Url.ToString();
Application["errorMsg"] = objErr.Message;
Server.ClearError();
Response.Redirect("Error.aspx",true);//有时候会报异常:无法在发送 HTTP 标头之后进行重定向。
} void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
try
{
string str = DateTime.Now.ToString("yyyyMMdd");
int da = int.Parse(str);
if (da - 20081113 >= 30)//如果试用期已到,转到错误页面,提示用户注册软件。
{
Application["zc"] = "0";
throw new Exception("试用期已到,请注册软件!");
}
else
Application["zc"] = "1";
}
catch (Exception e2)
{ throw e2;
}
} void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。 }
</script>
{
/*
if (Application["zc"] != null)
{
if (Application["zc"].ToString() == "0")
{
Response.Flush();
Response.Redirect("Error.aspx?id=4");
}
}
*/
if (Session["info"] != null)
Session["info"] = "";
//检查登录状态,如果用户已经登录,给出提示信息
if (Session["LogState"] != null)
if (Session["LogState"].ToString() == "1")
Response.Redirect("error.aspx?id=2");
txtUser.Focus();
}
如果放到global.asax,用户看到提示信息后,再手工输入login.aspx的话,就会跳过session_start了。 try
{
string str = DateTime.Now.ToString("yyyyMMdd");
int da = int.Parse(str);
if (da - 20081113 >= 30)//如果试用期已到,转到错误页面,提示用户注册软件。
{
Application["zc"] = "0";
throw new Exception("试用期已到,请注册软件!");
}
else
Application["zc"] = "1";
}
catch (Exception e2)
{ throw e2;
}