using System; using System.Web.Mvc; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Web.Models; using Umbraco.Web.Routing; namespace Umbraco.Web.Mvc { /// /// The default controller to render front-end requests /// [PreRenderViewActionFilter] public class RenderMvcController : UmbracoController, IRenderMvcController { public RenderMvcController() : base() { ActionInvoker = new RenderActionInvoker(); } public RenderMvcController(UmbracoContext umbracoContext, UmbracoHelper umbracoHelper) : base(umbracoContext, umbracoHelper) { } public RenderMvcController(UmbracoContext umbracoContext) : base(umbracoContext) { } private PublishedContentRequest _publishedContentRequest; /// /// Returns the current UmbracoContext /// public override UmbracoContext UmbracoContext { get { return PublishedContentRequest.RoutingContext.UmbracoContext; } } /// /// Returns the Current published content item for rendering the content /// protected IPublishedContent CurrentPage { get { return PublishedContentRequest.PublishedContent; } } /// /// Returns the current PublishedContentRequest /// protected internal virtual PublishedContentRequest PublishedContentRequest { get { if (_publishedContentRequest != null) return _publishedContentRequest; if (RouteData.DataTokens.ContainsKey(Core.Constants.Web.PublishedDocumentRequestDataToken) == false) { throw new InvalidOperationException("DataTokens must contain an 'umbraco-doc-request' key with a PublishedContentRequest object"); } _publishedContentRequest = (PublishedContentRequest)RouteData.DataTokens[Core.Constants.Web.PublishedDocumentRequestDataToken]; return _publishedContentRequest; } } /// /// Checks to make sure the physical view file exists on disk /// /// /// protected bool EnsurePhsyicalViewExists(string template) { var result = ViewEngines.Engines.FindView(ControllerContext, template, null); if (result.View == null) { LogHelper.Warn("No physical template file was found for template " + template); return false; } return true; } /// /// Returns an ActionResult based on the template name found in the route values and the given model. /// /// /// /// /// /// If the template found in the route values doesn't physically exist, then an empty ContentResult will be returned. /// protected ActionResult CurrentTemplate(T model) { var template = ControllerContext.RouteData.Values["action"].ToString(); if (EnsurePhsyicalViewExists(template) == false) throw new Exception("No physical template file was found for template " + template); return View(template, model); } /// /// The default action to render the front-end view /// /// /// [RenderIndexActionSelector] public virtual ActionResult Index(RenderModel model) { return CurrentTemplate(model); } } }