mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-12-02 11:44:41 +08:00
Handling NotFound custom pages
--HG-- branch : 1.x
This commit is contained in:
36
src/Orchard.Web/Core/Common/Controllers/ErrorController.cs
Normal file
36
src/Orchard.Web/Core/Common/Controllers/ErrorController.cs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
36
src/Orchard.Web/Core/Common/Routes.cs
Normal file
36
src/Orchard.Web/Core/Common/Routes.cs
Normal file
@@ -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<RouteDescriptor> routes) {
|
||||
foreach (var routeDescriptor in GetRoutes())
|
||||
routes.Add(routeDescriptor);
|
||||
}
|
||||
|
||||
public IEnumerable<RouteDescriptor> 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())
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -65,6 +65,7 @@
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Common\Controllers\ErrorController.cs" />
|
||||
<Compile Include="Common\DateEditor\DateEditorSettings.cs" />
|
||||
<Compile Include="Common\OwnerEditor\OwnerEditorSettings.cs" />
|
||||
<Compile Include="Common\OwnerEditor\OwnerEditorDriver.cs" />
|
||||
@@ -76,6 +77,7 @@
|
||||
<Compile Include="Common\Models\IdentityPartRecord.cs" />
|
||||
<Compile Include="Common\Models\IdentityPart.cs" />
|
||||
<Compile Include="Common\ResourceManifest.cs" />
|
||||
<Compile Include="Common\Routes.cs" />
|
||||
<Compile Include="Common\Services\XmlRpcHandler.cs" />
|
||||
<Compile Include="Common\DateEditor\DateEditorViewModel.cs" />
|
||||
<Compile Include="Common\Settings\TextFieldSettingsEvents.cs" />
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
@model dynamic
|
||||
@*
|
||||
@Model.RequestedUrl
|
||||
@Model.ReferrerUrl
|
||||
*@
|
||||
|
||||
<h1>@Html.TitleForPage(T("Not found").ToString())</h1>
|
||||
<p>@T("The page you are looking for does not exist.")</p>
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user