From ba4c3030ffbbb89d38f2764f3f0c951efb0cb460 Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Mon, 17 Apr 2017 14:47:47 -0700 Subject: [PATCH] Fixed NotFound method not logging correct info/returning 404 status --- .../Error/Controllers/ErrorController.cs | 23 ++++--------- Teknik/Controllers/DefaultController.cs | 32 +++++++++++++++---- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/Teknik/Areas/Error/Controllers/ErrorController.cs b/Teknik/Areas/Error/Controllers/ErrorController.cs index 3f532bf..bb41767 100644 --- a/Teknik/Areas/Error/Controllers/ErrorController.cs +++ b/Teknik/Areas/Error/Controllers/ErrorController.cs @@ -145,7 +145,12 @@ namespace Teknik.Areas.Error.Controllers errorMessage += " for page: " + Request.Url.AbsoluteUri; } - errorMessage += " - using Method: " + Request.HttpMethod; + if (Request.UrlReferrer != null) + { + errorMessage += " | for referred page: " + Request.Url.AbsoluteUri; + } + + errorMessage += " | using Method: " + Request.HttpMethod; } Logger.WriteEntry(LogLevel.Warning, errorMessage, exception); @@ -182,21 +187,5 @@ namespace Teknik.Areas.Error.Controllers return View("~/Areas/Error/Views/Error/Http500.cshtml", model); } - - private string GetIPAddress() - { - string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; - - if (!string.IsNullOrEmpty(ipAddress)) - { - string[] addresses = ipAddress.Split(','); - if (addresses.Length != 0) - { - return addresses[0]; - } - } - - return Request.ServerVariables["REMOTE_ADDR"]; - } } } \ No newline at end of file diff --git a/Teknik/Controllers/DefaultController.cs b/Teknik/Controllers/DefaultController.cs index f124079..206b21d 100644 --- a/Teknik/Controllers/DefaultController.cs +++ b/Teknik/Controllers/DefaultController.cs @@ -1,6 +1,7 @@ using System; using System.Web; using System.Web.Mvc; +using System.Web.Routing; using Teknik.Areas.Error.Controllers; using Teknik.Configuration; @@ -36,6 +37,8 @@ namespace Teknik.Controllers get { return HttpContext.User as TeknikPrincipal; } } + public object ObjectFactory { get; private set; } + public DefaultController() { ViewBag.Title = Config.Title; @@ -47,6 +50,28 @@ namespace Teknik.Controllers } } + protected override void HandleUnknownAction(string actionName) + { + this.InvokeHttp404(HttpContext); + } + + [HttpGet] + [AllowAnonymous] + public ActionResult InvokeHttp404(HttpContextBase httpContext) + { + IController errorController = new ErrorController(); + var errorRoute = new RouteData(); + errorRoute.DataTokens.Add("namespaces", new[] { typeof(ErrorController).Namespace }); + errorRoute.DataTokens.Add("area", "Error"); + errorRoute.Values.Add("controller", "Error"); + errorRoute.Values.Add("action", "Http404"); + errorRoute.Values.Add("exception", null); + errorController.Execute(new RequestContext( + httpContext, errorRoute)); + + return new EmptyResult(); + } + // Get the Favicon [HttpGet] [AllowAnonymous] @@ -81,12 +106,7 @@ namespace Teknik.Controllers [AllowAnonymous] public ActionResult NotFound() { - var errorController = new ErrorController(); - if (errorController != null) - { - return errorController.Http404(new Exception("Page Not Found")); - } - return null; + return InvokeHttp404(HttpContext); } protected ActionResult GenerateActionResult(object json)