C# Web应用程序中的异常,ASP.NET MVC
我开发了一个ASP.NETMVC应用程序。如果发生错误,我想将用户重定向到错误页面。但如果在应用程序初始化时发生错误,我无法重定向用户,因为“响应在此上下文中不可用”。是我的代码:C# Web应用程序中的异常,ASP.NET MVC,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我开发了一个ASP.NETMVC应用程序。如果发生错误,我想将用户重定向到错误页面。但如果在应用程序初始化时发生错误,我无法重定向用户,因为“响应在此上下文中不可用”。是我的代码: protected void Application_Start() { //... throw new Exception("qwerty"); } protected void Application_Error(object sender, E
protected void Application_Start()
{
//...
throw new Exception("qwerty");
}
protected void Application_Error(object sender, EventArgs e)
{
// Here I have got an HttpException with message
// "Response is not available in this context"
HttpContext.Current.Response.RedirectToRoute("Default");
}
如果错误发生在应用程序初始化时,是否有方法将用户重定向到错误页面?
如果错误发生在应用程序初始化时
您的意思是“在开始服务web请求之前”?@mjwills我的意思是“错误发生在应用程序启动方法中”。英语不是我的母语,我可能会在措辞上出错。当我在try/catch块应用程序中包装这行时,@mjwills可能重复。在catch块中,我捕获HttpExceptionSo尝试{HttpContext.Current.Response.RedirectToRoute(“默认”);}catch{}
不起作用?