3

I know of a couple ways to catch 404 and 500 errors. For example, I can use web.config, route.config, global.asax, and a controller; however, I don't where to put the code to make it catch these errors. Let me tell you what I've done.

Route Config:

The route.config works for 404 errors, but it won't work for 500 errors (to my knowledge). Regardless, I DON'T want to use it because I've heard that it has some downsides to it. Also, it seems to be a very poor solution to this problem IMO.

web/Web Config:

I have tried every possible web config file I have: web.config (system.web and system.webServer) and Web.config (system.web and system.webServer) as well as the Web Debug File (system.web and system.webServer). ALL of these didn't catch the error (It frustrates me because no one will tell me where EXACTLY to put the code so it catches. i.e., place1 -> place2 -> place3 -> etc... Every answer gives me the code and either says Web.config or web.config - I know that but WHERE in Web.config or web.config?) I heard this way has restrictions, but they aren't relevant to me. I know it only works for IIS 7+. I think my team and I are using IIS 8, so that shouldn't be a problem for me. I prefer a method in global.asax or web.config/Web.config.

Global Asax:

I am using the two application error handler methods Application_EndRequest and Application_Error, both of which aren't working. The only error I get is a 200 error (this error is only caught by EndRequest), which isn't an error but quite the opposite. I don't know why I would be getting this when my page shows a 404 error. To my knowledge, it's because Global Asax isn't programmed to catch these errors, but web.config and/or Web.config, and as you know, I don't know how to make that work. I will accept a global.asax solution because I haven't heard anything bad about it, yet.

Controller:

I only tried one solution: protected override void HandleUnknownAction(string actionName), and the solution called for a route in route.config {*url}. Surprise, surprise, this didn't work either (as expected). I learned that this solution using the code {*url} to find 404 errors (or at least it was the solution I searched.) And as I said earlier, I don't want a route.config solution. If I am correct, protected override void HandleUnknownAction(string actionName) may work in global but not for me.

Tried Solutions:

  1. protected override void HandleUnknownAction(string actionName)

  2. protected void Application_EndRequest()

  3. protected void Application_Error(object sender, EventArgs e)

  4. protected override void OnException(ExceptionContext filterContext)

5.

<system.webServer>
   <httpErrors errorMode="Custom" defaultResponseMode="File" >
      <remove statusCode="404" />
      <remove statusCode="500" />
      <error statusCode="404" 
         path="404.html" />
      <error statusCode="500" 
         path="500.html" />
    </httpErrors>
</system.webServer>

6.

<httpErrors>
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" prefixLanguageFilePath="" 
    path="http://yoursite.com/index.asp?syserror" responseMode="Redirect" />
</httpErrors>

7.

<customErrors mode="On">
    <error statusCode="404" redirect="/Custom404.html" />
    <error statusCode="500" redirect="/Custom500.html" />
</customErrors>

8.

 routes.MapRoute(
     "Error", // Route name
     "Error/{errorCode}", // URL with parameters
     new { controller = "Page", action = "Error", errorCode= UrlParameter.Optional }
 );

9.

 if (Context.Response.StatusCode == 404)
 {
     Response.Clear();

     var rd = new RouteData();
     rd.DataTokens["area"] = "AreaName"; // In case controller is in another area
     rd.Values["controller"] = "Errors";
     rd.Values["action"] = "NotFound";

     IController c = new ErrorsController();
     c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
 }

10.

Exception exception = Server.GetLastError();
// Log the exception.

ILogger logger = Container.Resolve<ILogger>();
logger.Error(exception);

Response.Clear();

HttpException httpException = exception as HttpException;

RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");

if (httpException == null)
{
    routeData.Values.Add("action", "Index");
}
else //It's an Http Exception, Let's handle it.
{
    switch (httpException.GetHttpCode())
    {
       case 404:
           // Page not found.
           routeData.Values.Add("action", "HttpError404");
           break;
       case 500:
           // Server error.
           routeData.Values.Add("action", "HttpError500");
           break;

        // Here you can handle Views to other error codes.
        // I choose a General error template  
        default:
           routeData.Values.Add("action", "General");
           break;
   }
}

Note: Firstly, I may have adjusted some of this solutions to fit my code, Secondly, I take no credit for this solutions. I found most, if not all, of the solutions on other forum pages. Thirdly, the only solution that worked is the 8th one; however, I believe it only works for 404s. Nevertheless, I don't want to use it because I believe it is a bad solution (correct me if I'm wrong.)

Conclusion:

I am NOT asking you to solve the solution for me. I simply need two thing: one, I need to be corrected if I was misinformed (through a comment or answer); and two, I need to know WHERE to put the code and the result of the code (through either a picture or an explanation.) If you put something like the following:

Here is code that worked for me

[Insert Code Here]

[Insert more description]

I will most likely copy the code, change it, try it, and inevitably get upset if/when it fails. If you could take the time to explain how 404 errors are caught and a global.asax or web/Web Config Solution, I will greatly appreciate it. I have been struggling with this problem for a while now, and I have put in a lot of time and effort into it, only to get vague solutions with little to no explanation as to why it catches 404/500 errors or where to put it, exactly.

Edit:

Here are the error methods I want to hit. I can assure you that they are routed in my route.config using routes.MapMvcAttributeRoutes();

[Route("~/page_not_found")]
public ActionResult PageNotFound()
{
    Response.StatusCode = 404;
    return View();
}
[Route("~/internal_server_error")]
public ActionResult InternalServerError()
{
    Response.StatusCode = 500;
    return View();
}

1 Answers1

0

my experience was in this direction. In terms of user experience, 500 redirects are made so that he does not see the wrong page. but redirecting 500 does not send exception parameters to the redirected page. To log the error with 500, it is necessary to use global.asax.