mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Updating use of routable path constraint
--HG-- branch : dev
This commit is contained in:
@@ -12,15 +12,21 @@ namespace Orchard.Core.Routable.Controllers {
|
|||||||
[ValidateInput(false)]
|
[ValidateInput(false)]
|
||||||
public class ItemController : Controller {
|
public class ItemController : Controller {
|
||||||
private readonly IContentManager _contentManager;
|
private readonly IContentManager _contentManager;
|
||||||
|
private readonly IRoutablePathConstraint _routablePathConstraint;
|
||||||
|
|
||||||
public ItemController(IContentManager contentManager) {
|
public ItemController(IContentManager contentManager, IRoutablePathConstraint routablePathConstraint) {
|
||||||
_contentManager = contentManager;
|
_contentManager = contentManager;
|
||||||
|
_routablePathConstraint = routablePathConstraint;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult Display(string path) {
|
public ActionResult Display(string path) {
|
||||||
|
var matchedPath = _routablePathConstraint.FindPath(path);
|
||||||
|
if (string.IsNullOrEmpty(matchedPath)) {
|
||||||
|
throw new ApplicationException("404 - should not have passed path constraint");
|
||||||
|
}
|
||||||
var hits = _contentManager
|
var hits = _contentManager
|
||||||
.Query<IsRoutable, RoutableRecord>(VersionOptions.Published)
|
.Query<IsRoutable, RoutableRecord>(VersionOptions.Published)
|
||||||
.Where(r => r.Path == path)
|
.Where(r => r.Path == matchedPath)
|
||||||
.Slice(0, 2);
|
.Slice(0, 2);
|
||||||
if (hits.Count() == 0) {
|
if (hits.Count() == 0) {
|
||||||
throw new ApplicationException("404 - should not have passed path constraint");
|
throw new ApplicationException("404 - should not have passed path constraint");
|
||||||
@@ -34,7 +40,7 @@ namespace Orchard.Core.Routable.Controllers {
|
|||||||
PrepareDisplayViewModel(model.Routable);
|
PrepareDisplayViewModel(model.Routable);
|
||||||
return View("Display", model);
|
return View("Display", model);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void PrepareDisplayViewModel(ContentItemViewModel<IRoutableAspect> itemViewModel) {
|
private void PrepareDisplayViewModel(ContentItemViewModel<IRoutableAspect> itemViewModel) {
|
||||||
if (string.IsNullOrEmpty(itemViewModel.TemplateName)) {
|
if (string.IsNullOrEmpty(itemViewModel.TemplateName)) {
|
||||||
itemViewModel.TemplateName = "Items/Contents.Item";
|
itemViewModel.TemplateName = "Items/Contents.Item";
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ namespace Orchard.Core.Routable.Drivers {
|
|||||||
part.Record.Path = "routable" + part.ContentItem.Id;
|
part.Record.Path = "routable" + part.ContentItem.Id;
|
||||||
return base.Editor(part, updater);
|
return base.Editor(part, updater);
|
||||||
}
|
}
|
||||||
|
|
||||||
//private const string TemplateName = "Parts/Common.Routable";
|
//private const string TemplateName = "Parts/Common.Routable";
|
||||||
|
|
||||||
//private readonly IOrchardServices _services;
|
//private readonly IOrchardServices _services;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace Orchard.Core.Routable.Services {
|
|||||||
/// Singleton object, per Orchard Shell instance. We need to protect concurrent access to the dictionary.
|
/// Singleton object, per Orchard Shell instance. We need to protect concurrent access to the dictionary.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly object _syncLock = new object();
|
private readonly object _syncLock = new object();
|
||||||
private IDictionary<string, string> _slugs = new Dictionary<string, string>();
|
private IDictionary<string, string> _paths = new Dictionary<string, string>();
|
||||||
|
|
||||||
public RoutablePathConstraint() {
|
public RoutablePathConstraint() {
|
||||||
Logger = NullLogger.Instance;
|
Logger = NullLogger.Instance;
|
||||||
@@ -21,31 +21,31 @@ namespace Orchard.Core.Routable.Services {
|
|||||||
|
|
||||||
public ILogger Logger { get; set; }
|
public ILogger Logger { get; set; }
|
||||||
|
|
||||||
public void SetSlugs(IEnumerable<string> slugs) {
|
public void SetPaths(IEnumerable<string> paths) {
|
||||||
// Make a copy to avoid performing potential lazy computation inside the lock
|
// Make a copy to avoid performing potential lazy computation inside the lock
|
||||||
var slugsArray = slugs.ToArray();
|
var slugsArray = paths.ToArray();
|
||||||
|
|
||||||
lock (_syncLock) {
|
lock (_syncLock) {
|
||||||
_slugs = slugsArray.Distinct(StringComparer.OrdinalIgnoreCase).ToDictionary(value => value, StringComparer.OrdinalIgnoreCase);
|
_paths = slugsArray.Distinct(StringComparer.OrdinalIgnoreCase).ToDictionary(value => value, StringComparer.OrdinalIgnoreCase);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string FindSlug(string slug) {
|
public string FindPath(string path) {
|
||||||
lock (_syncLock) {
|
lock (_syncLock) {
|
||||||
string actual;
|
string actual;
|
||||||
return _slugs.TryGetValue(slug, out actual) ? actual : slug;
|
return _paths.TryGetValue(path, out actual) ? actual : path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddSlug(string slug) {
|
public void AddSlug(string slug) {
|
||||||
lock (_syncLock) {
|
lock (_syncLock) {
|
||||||
_slugs[slug] = slug;
|
_paths[slug] = slug;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveSlug(string slug) {
|
public void RemoveSlug(string slug) {
|
||||||
lock (_syncLock) {
|
lock (_syncLock) {
|
||||||
_slugs.Remove(slug);
|
_paths.Remove(slug);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ namespace Orchard.Core.Routable.Services {
|
|||||||
var parameterValue = Convert.ToString(value);
|
var parameterValue = Convert.ToString(value);
|
||||||
|
|
||||||
lock (_syncLock) {
|
lock (_syncLock) {
|
||||||
return _slugs.ContainsKey(parameterValue);
|
return _paths.ContainsKey(parameterValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user