我正在开发一个基于 API 的网站,客户端正在.Net MVC 中开发。对于异常处理,我正在使用
public void Application_Error(object sender, EventArgs e)
{
string action = "Index";
Exception exception = Server.GetLastError();
Response.Clear();
HttpException httpException = exception as HttpException;
if (httpException != null)
{
switch (httpException.GetHttpCode())
{
case 404:
// page not found
action = "Error404";
break;
default:
action = "Index";
break;
}
// clear error on server
Server.ClearError();
}
Response.Redirect(String.Format("/error/{0}", action));
}
因此对于控制器的 try catch 抛出的任何异常,页面都会重定向到错误页面。
现在我希望当会话过期时它应该重定向到登录页面,我该怎么做?
现在发生的事情是,在会话到期后,当我尝试访问会话值时,它会抛出异常“ object reference not set to an instance of object
。” 然后它重定向到默认错误页面。