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

后端 未结 11 761
夕颜
夕颜 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 03:47

    I think you'd instead implement/use the Application_Error event in Global.asax, and do your processing/redirects there.

    Providing you call Server.ClearError in that handler, I don't think it will use the customErrors config at all.

    0 讨论(0)
  • 2020-12-29 03:51

    In the global.asax, catch the 404 error and redirect to the file not found page. I didn't require the aspxerrorpath and it worked a treat for me.

    void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();
        if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
        {
            Response.Redirect("~/filenotfound.aspx");
        }
        else
        {
            // your global error handling here!
        }
    }
    
    0 讨论(0)
  • 2020-12-29 03:55

    In my case, i prefer not use Web.config. Then i created code above in Global.asax file:

    protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError();
    
            //Not Found (When user digit unexisting url)
            if(ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
            {
                HttpContextWrapper contextWrapper = new HttpContextWrapper(this.Context);
    
                RouteData routeData = new RouteData();
                routeData.Values.Add("controller", "Error");
                routeData.Values.Add("action", "NotFound");
    
                IController controller = new ErrorController();
                RequestContext requestContext = new RequestContext(contextWrapper, routeData);
                controller.Execute(requestContext);
                Response.End();
            }
            else //Unhandled Errors from aplication
            {
                ErrorLogService.LogError(ex);
                HttpContextWrapper contextWrapper = new HttpContextWrapper(this.Context);
    
                RouteData routeData = new RouteData();
                routeData.Values.Add("controller", "Error");
                routeData.Values.Add("action", "Index");
    
                IController controller = new ErrorController();
                RequestContext requestContext = new RequestContext(contextWrapper, routeData);
                controller.Execute(requestContext);
                Response.End();
            }
        }
    

    And thtat is my ErrorController.cs

    public class ErrorController : Controller
    {
        // GET: Error
        public ViewResult Index()
        {
            Response.StatusCode = 500;
            Exception ex = Server.GetLastError();
            return View("~/Views/Shared/SAAS/Error.cshtml", ex);
        }
    
        public ViewResult NotFound()
        {
            Response.StatusCode = 404;
            return View("~/Views/Shared/SAAS/NotFound.cshtml");
        }
    }
    

    And that is my ErrorLogService.cs

    //common service to be used for logging errors
    public static class ErrorLogService
    {
        public static void LogError(Exception ex)
        {
            //Do what you want here, save log in database, send email to police station
        }
    }
    
    0 讨论(0)
  • 2020-12-29 03:56

    If you remove aspxerrorpath=/ and you use response redirect during error handling you'll get exception there will be redirection loop.

    0 讨论(0)
  • 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

    <customErrors mode="On" defaultRedirect="~/error/500">
      <error statusCode="404" redirect="~/error/404"/>
    </customErrors>
    

    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");
        }
    }
    
    0 讨论(0)
提交回复
热议问题