How to prevent “aspxerrorpath” being passed as a query string to ASP.NET custom error pages

后端 未结 11 858
夕颜
夕颜 2020-12-29 03:36

In my ASP.NET web application, I have defined custom error pages in my web.config file as follows:



        
11条回答
  •  情话喂你
    2020-12-29 04:05

    The best solution (more a workaround..) I implemented since now to prevent aspxerrorpath issue continuing to use ASP.NET CustomErrors support, is redirect to the action that implements Error handling.

    These are some step of my solution in an ASP.NET MVC web app context:

    First enable custom errors module in web.config

    
      
    
    

    Then define a routing rule:

    routes.MapRoute(
       name: "Error",
       url: "error/{errorType}/{aspxerrorpath}",
       defaults: new { controller = "Home", action = "Error", errorType = 500, aspxerrorpath = UrlParameter.Optional },
    );
    

    Finally implement following action (and related views..):

    public ActionResult Error(int errorType, string aspxerrorpath)
    {
       if (!string.IsNullOrEmpty(aspxerrorpath)) {
          return RedirectToRoute("Error", errorType);
       }
    
       switch (errorType) {
          case 404:
               return View("~/Views/Shared/Errors/404.cshtml");
    
           case 500:
            default:
               return View("~/Views/Shared/Errors/500.cshtml");
        }
    }
    

提交回复
热议问题