如何更改UnauthorizedAccessException在ASP.Net中重定向到的位置

时间:2012-08-04 14:00:31

标签: c# asp.net unauthorizedaccessexcepti

非常简单。
我在AuthorizationFilter中抛出了UnauthorizedAccessException。我希望UnauthorizedAccessException转到错误页面,而不是/ Account / Login页面 我怎样才能做出改变?

2 个答案:

答案 0 :(得分:3)

尝试在global.asax.cs

中设置类似的内容
protected void Application_Error(object sender, EventArgs e)
{
    // excpt is the exception thrown
    // The exception that really happened is retrieved through the GetBaseException() method
    // because the exception returned by the GetLastError() is a HttpException
    Exception excpt = Server.GetLastError().GetBaseException();

      if(excpt is UnauthorizedAccessException)
      { 
        // redirect here
      }

}

答案 1 :(得分:1)

您可以使用多个异常处理程序

        try
        {
            // code here
        }
        catch (UnauthorizedAccessException)
        {
            Response.Redirect(errorPageUrl, false);
        }
        catch (Exception)
        {
            Response.Redirect(loginPageUrl, false);
        }