ASP.NET MVC4 CustomErrors DefaultRedirect Ignored

后端 未结 6 1914
悲&欢浪女
悲&欢浪女 2021-02-04 07:50

I have an MVC 4 app, using a custom HandleErrorAttribute to handle only custom exceptions. I would like to intercept the default 404 and other non-500 error pages and replace t

6条回答
  •  忘了有多久
    2021-02-04 08:16

    This should work :

    1. Web.Config

    
    
      
    
      
    
    
    

    2. Registered HandleErrorAttribute as a global action filter in the FilterConfig class as follows

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new CustomHandleErrorAttribute());
            filters.Add(new AuthorizeAttribute());
        }
    

    If that dont work then, Try to make yourself transfer the response by checking status codes like the Following in the Global.asax: at least it must work.

    void Application_EndRequest(object sender, EventArgs e)
    {
        if (Response.StatusCode == 401)
        {
            Response.ClearContent();
            Server.Transfer("~/Views/Shared/UnauthorizedAccess.cshtml");
        }
    }
    

提交回复
热议问题