Skip to main content
 首页 » 编程设计

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

2024年02月20日10over140

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

请您参考如下方法:

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