如何更改ASP中UnauthorizedAccessException重定向到的位置网

本文关键字:位置 重定向 UnauthorizedAccessException 何更改 ASP | 更新日期: 2024-10-24 15:40:41

非常简单
我正在AuthorizationFilter中引发UnauthorizedAccessException。我希望UnauthorizedAccessException指向错误页面,而不是/Account/Login页面
我该如何改变?

如何更改ASP中UnauthorizedAccessException重定向到的位置网

尝试在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
      }
}

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

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