From 435271be217f7a12a0463ed4b306b90194bc27bb Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Tue, 31 Jan 2012 16:05:07 -0800 Subject: [PATCH] Handling NotFound custom pages --HG-- branch : 1.x --- .../Common/Controllers/ErrorController.cs | 36 +++++++++++++++++++ src/Orchard.Web/Core/Common/Routes.cs | 36 +++++++++++++++++++ src/Orchard.Web/Core/Orchard.Core.csproj | 2 ++ .../Core/Shapes/Views/NotFound.cshtml | 7 +++- .../Filters/UnhandledExceptionFilter.cs | 21 +++++++++++ 5 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/Orchard.Web/Core/Common/Controllers/ErrorController.cs create mode 100644 src/Orchard.Web/Core/Common/Routes.cs diff --git a/src/Orchard.Web/Core/Common/Controllers/ErrorController.cs b/src/Orchard.Web/Core/Common/Controllers/ErrorController.cs new file mode 100644 index 000000000..0b43dc921 --- /dev/null +++ b/src/Orchard.Web/Core/Common/Controllers/ErrorController.cs @@ -0,0 +1,36 @@ +using System.Net; +using System.Web.Mvc; +using Orchard.Mvc; +using Orchard.Themes; + +namespace Orchard.Core.Common.Controllers { + [Themed] + public class ErrorController : Controller { + private readonly IOrchardServices _orchardServices; + + public ErrorController(IOrchardServices orchardServices) { + _orchardServices = orchardServices; + } + + public ActionResult NotFound(string url) { + Response.StatusCode = (int)HttpStatusCode.NotFound; + var model = _orchardServices.New.NotFound(); + + if(url == null) { + url = Request.Url.OriginalString; + } + + // If the url is relative then replace with Requested path + model.RequestedUrl = Request.Url.OriginalString.Contains(url) & Request.Url.OriginalString != url ? + Request.Url.OriginalString : url; + + // Dont get the user stuck in a 'retry loop' by + // allowing the Referrer to be the same as the Request + model.ReferrerUrl = Request.UrlReferrer != null && + Request.UrlReferrer.OriginalString != model.RequestedUrl ? + Request.UrlReferrer.OriginalString : null; + + return new ShapeResult(this, model); + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Core/Common/Routes.cs b/src/Orchard.Web/Core/Common/Routes.cs new file mode 100644 index 000000000..8f8a94535 --- /dev/null +++ b/src/Orchard.Web/Core/Common/Routes.cs @@ -0,0 +1,36 @@ +using System.Collections.Generic; +using System.Web; +using System.Web.Mvc; +using System.Web.Routing; +using Orchard.Mvc.Routes; + +namespace Orchard.Core.Common { + public class Routes : IRouteProvider { + public void GetRoutes(ICollection routes) { + foreach (var routeDescriptor in GetRoutes()) + routes.Add(routeDescriptor); + } + + public IEnumerable GetRoutes() { + return new[] { + new RouteDescriptor { + Priority = -9999, + Route = new Route( + "{*path}", + new RouteValueDictionary { + {"area", "Common"}, + {"controller", "Error"}, + {"action", "NotFound"} + }, + new RouteValueDictionary { + }, + new RouteValueDictionary { + {"area", "Common"} + }, + new MvcRouteHandler()) + } + }; + } + + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Core/Orchard.Core.csproj b/src/Orchard.Web/Core/Orchard.Core.csproj index 091eaba2e..232ca8e45 100644 --- a/src/Orchard.Web/Core/Orchard.Core.csproj +++ b/src/Orchard.Web/Core/Orchard.Core.csproj @@ -65,6 +65,7 @@ + @@ -76,6 +77,7 @@ + diff --git a/src/Orchard.Web/Core/Shapes/Views/NotFound.cshtml b/src/Orchard.Web/Core/Shapes/Views/NotFound.cshtml index 21a5c7292..dbf713254 100644 --- a/src/Orchard.Web/Core/Shapes/Views/NotFound.cshtml +++ b/src/Orchard.Web/Core/Shapes/Views/NotFound.cshtml @@ -1,3 +1,8 @@ -@model dynamic +@* + @Model.RequestedUrl + @Model.ReferrerUrl +*@ +

@Html.TitleForPage(T("Not found").ToString())

@T("The page you are looking for does not exist.")

+ diff --git a/src/Orchard/Exceptions/Filters/UnhandledExceptionFilter.cs b/src/Orchard/Exceptions/Filters/UnhandledExceptionFilter.cs index 8db9ff54f..f9adf1563 100644 --- a/src/Orchard/Exceptions/Filters/UnhandledExceptionFilter.cs +++ b/src/Orchard/Exceptions/Filters/UnhandledExceptionFilter.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Net; +using System.Web; using System.Web.Mvc; using Orchard.Logging; using Orchard.Mvc; @@ -56,6 +58,25 @@ namespace Orchard.Exceptions.Filters { } } } + + if (filterContext.Result is HttpNotFoundResult) { + var model = _orchardServices.New.NotFound(); + var request = filterContext.RequestContext.HttpContext.Request; + var url = request.RawUrl; + + // If the url is relative then replace with Requested path + model.RequestedUrl = request.Url.OriginalString.Contains(url) & request.Url.OriginalString != url ? + request.Url.OriginalString : url; + + // Dont get the user stuck in a 'retry loop' by + // allowing the Referrer to be the same as the Request + model.ReferrerUrl = request.UrlReferrer != null && + request.UrlReferrer.OriginalString != model.RequestedUrl ? + request.UrlReferrer.OriginalString : null; + + filterContext.Result = new ShapeResult(filterContext.Controller, model); + filterContext.RequestContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound; + } } } }