using System; using System.IO; using System.Web.Mvc; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Web.Models; using Umbraco.Web.Routing; namespace Umbraco.Web.Mvc { public class RenderMvcController : Controller { public RenderMvcController() { ActionInvoker = new RenderActionInvoker(); } private PublishedContentRequest _publishedContentRequest; /// /// Returns the current UmbracoContext /// protected UmbracoContext UmbracoContext { get { return PublishedContentRequest.RoutingContext.UmbracoContext; } } /// /// Returns the Current published content item for rendering the content /// protected IPublishedContent CurrentPage { get { return PublishedContentRequest.PublishedContent; } } //TODO: make this protected once we make PublishedContentRequest not internal after we figure out what it should actually contain /// /// Returns the current PublishedContentRequest /// internal PublishedContentRequest PublishedContentRequest { get { if (_publishedContentRequest != null) return _publishedContentRequest; if (!RouteData.DataTokens.ContainsKey("umbraco-doc-request")) { throw new InvalidOperationException("DataTokens must contain an 'umbraco-doc-request' key with a PublishedContentRequest object"); } _publishedContentRequest = (PublishedContentRequest) RouteData.DataTokens["umbraco-doc-request"]; return _publishedContentRequest; } } /// /// Checks to make sure the physical view file exists on disk /// /// /// protected bool EnsurePhsyicalViewExists(string template) { if (!System.IO.File.Exists( Path.Combine(Server.MapPath(Constants.ViewLocation), template + ".cshtml"))) { 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)) { return Content(""); } return View(template, model); } /// /// The default action to render the front-end view /// /// /// public virtual ActionResult Index(RenderModel model) { return CurrentTemplate(model); } } }