Files
Orchard/src/Orchard.Web/Core/Routable/Controllers/ItemController.cs
T

51 lines
2.1 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Web.Mvc;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.Core.Common.Models;
using Orchard.Core.Routable.Models;
using Orchard.Core.Routable.ViewModels;
using Orchard.Mvc.ViewModels;
namespace Orchard.Core.Routable.Controllers {
[ValidateInput(false)]
public class ItemController : Controller {
private readonly IContentManager _contentManager;
2010-06-08 11:01:39 -07:00
private readonly IRoutablePathConstraint _routablePathConstraint;
2010-06-08 11:01:39 -07:00
public ItemController(IContentManager contentManager, IRoutablePathConstraint routablePathConstraint) {
_contentManager = contentManager;
2010-06-08 11:01:39 -07:00
_routablePathConstraint = routablePathConstraint;
}
public ActionResult Display(string path) {
2010-06-08 11:01:39 -07:00
var matchedPath = _routablePathConstraint.FindPath(path);
if (string.IsNullOrEmpty(matchedPath)) {
throw new ApplicationException("404 - should not have passed path constraint");
}
var hits = _contentManager
.Query<IsRoutable, RoutableRecord>(VersionOptions.Published)
2010-06-08 11:01:39 -07:00
.Where(r => r.Path == matchedPath)
.Slice(0, 2);
if (hits.Count() == 0) {
throw new ApplicationException("404 - should not have passed path constraint");
}
if (hits.Count() != 1) {
throw new ApplicationException("Ambiguous content");
}
var model = new RoutableDisplayViewModel {
Routable = _contentManager.BuildDisplayModel<IRoutableAspect>(hits.Single(), "Detail")
};
PrepareDisplayViewModel(model.Routable);
return View("Display", model);
}
2010-06-08 11:01:39 -07:00
private void PrepareDisplayViewModel(ContentItemViewModel<IRoutableAspect> itemViewModel) {
if (string.IsNullOrEmpty(itemViewModel.TemplateName)) {
itemViewModel.TemplateName = "Items/Contents.Item";
}
}
}
}