diff --git a/src/Umbraco.Web/Routing/PublishedContentRequest.cs b/src/Umbraco.Web/Routing/PublishedContentRequest.cs index 7aeb79902b..95aad4cc92 100644 --- a/src/Umbraco.Web/Routing/PublishedContentRequest.cs +++ b/src/Umbraco.Web/Routing/PublishedContentRequest.cs @@ -56,7 +56,7 @@ namespace Umbraco.Web.Routing _engine = new PublishedContentRequestEngine(this); - this.RenderingEngine = RenderingEngine.Mvc; // default + this.RenderingEngine = RenderingEngine.Unknown; } /// @@ -124,7 +124,7 @@ namespace Umbraco.Web.Routing set { _publishedContent = value; - this.Template = null; + this.TemplateModel = null; _publishedContentId = _publishedContent != null ? _publishedContent.Id : 0; } } @@ -157,17 +157,92 @@ namespace Umbraco.Web.Routing #region Template - /// - /// Gets or sets the template to use to display the requested content. + /// + /// The template model, if any, else null. /// - public Template Template { get; set; } + //private Umbraco.Core.Models.ITemplate _templateModel; + private Template _template; + + /// + /// Gets or sets the template model to use to display the requested content. + /// + // NOTE - should use Umbraco.Core.Models.ITemplate/Template + // NOTE - this is for legacy, better use .Template / TrySetTemplate + //internal Umbraco.Core.Models.ITemplate TemplateModel + internal Template TemplateModel + { + get + { + return _template; + } + + set + { + _template = value; + } + } + + /// + /// Gets the template to use to display the requested content. + /// + public string Template + { + get + { + return _template == null ? null : _template.Alias; + } + } + + /// + /// Tries to set the template to use to display the requested content. + /// + /// The name of the template. + /// A value indicating whether a valid template with the specified name was found. + /// Setting the template resets RenderingEngine to Unknown. + public bool TrySetTemplate(string name) + { + this.RenderingEngine = Core.RenderingEngine.Unknown; // reset + + if (string.IsNullOrWhiteSpace(name)) + { + _template = null; + return true; + } + else + { + // NOTE - can we stil get it with whitespaces in it due to old legacy bugs? + name = name.Replace(" ", ""); + + var model = global::umbraco.cms.businesslogic.template.Template.GetByAlias(name); + var nmodel = ApplicationContext.Current.Services.FileService.GetTemplate(name); + if (model == null) + { + _template = null; + return false; + } + else + { + _template = model; + this.RenderingEngine = _engine.FindTemplateRenderingEngine(name); + + // Unkwnown means that no template was found. Default to Mvc because Mvc supports hijacking + // routes which sometimes doesn't require a template since the developer may want full control + // over the rendering. Can't do it in WebForms, so Mvc it is. And Mvc will also handle what to + // do if no template or hijacked route is exist. + if (this.RenderingEngine == RenderingEngine.Unknown) + this.RenderingEngine = RenderingEngine.Mvc; + + return true; + } + } + } /// /// Gets a value indicating whether the content request has a template. /// public bool HasTemplate { - get { return this.Template != null; } + get { return _template != null; } } #endregion diff --git a/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs b/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs index c15ee5825d..e88871549b 100644 --- a/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs +++ b/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs @@ -73,13 +73,13 @@ namespace Umbraco.Web.Routing // set the culture on the thread -- again, 'cos it might have changed due to a wildcard domain Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = _pcr.Culture; - // find the rendering engine - FindRenderingEngine(); - // trigger the Prepared event - at that point it is still possible to change about anything _pcr.OnPrepared(); - // set the culture on the thread -- again, 'cos it might have changed in the event handler + // we don't take care of anything xcept finding the rendering engine again + // so if the content has changed, it's up to the user to find out the template + + // set the culture on the thread -- again, 'cos it might have changed in the event handler Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = _pcr.Culture; // if request has been flagged to redirect then return @@ -120,7 +120,6 @@ namespace Umbraco.Web.Routing HandlePublishedContent(); // will go 404 FindTemplate(); - FindRenderingEngine(); // if request has been flagged to redirect then return // whoever called us is in charge of redirecting @@ -282,30 +281,6 @@ namespace Umbraco.Web.Routing } } - /// - /// Finds the rendering engine to use, and updates the PublishedContentRequest accordingly. - /// - internal void FindRenderingEngine() - { - RenderingEngine renderingEngine = RenderingEngine.Unknown; - - // NOTE: Not sure how the alias is actually saved with a space as this shouldn't ever be the case? - // but apparently this happens. I think what should actually be done always is the template alias - // should be saved using the ToUmbracoAlias method and then we can use this here too, that way it - // it 100% consistent. I'll leave this here for now until further invenstigation. - if (_pcr.HasTemplate) - renderingEngine = FindTemplateRenderingEngine(_pcr.Template.Alias.Replace(" ", "")); - - // Unkwnown means that no template was found. Default to Mvc because Mvc supports hijacking - // routes which sometimes doesn't require a template since the developer may want full control - // over the rendering. Can't do it in WebForms, so Mvc it is. And Mvc will also handle what to - // do if no template or hijacked route is exist. - if (renderingEngine == RenderingEngine.Unknown) - renderingEngine = RenderingEngine.Mvc; - - _pcr.RenderingEngine = renderingEngine; - } - #endregion #region Document and template @@ -546,7 +521,7 @@ namespace Umbraco.Web.Routing if (_pcr.PublishedContent == null) { - _pcr.Template = null; + _pcr.TemplateModel = null; return; } @@ -581,7 +556,7 @@ namespace Umbraco.Web.Routing var template = Template.GetTemplate(templateId); if (template == null) throw new InvalidOperationException("The template with Id " + templateId + " does not exist, the page cannot render"); - _pcr.Template = template; + _pcr.TemplateModel = template; LogHelper.Debug("{0}Got template id={1} alias=\"{2}\"", () => tracePrefix, () => template.Id, () => template.Alias); } else @@ -604,7 +579,7 @@ namespace Umbraco.Web.Routing var template = Template.GetByAlias(altTemplate, true); if (template != null) { - _pcr.Template = template; + _pcr.TemplateModel = template; LogHelper.Debug("{0}Got template id={1} alias=\"{2}\"", () => tracePrefix, () => template.Id, () => template.Alias); } else @@ -628,7 +603,7 @@ namespace Umbraco.Web.Routing } else { - LogHelper.Debug("{0}Running with template id={1} alias=\"{2}\"", () => tracePrefix, () => _pcr.Template.Id, () => _pcr.Template.Alias); + LogHelper.Debug("{0}Running with template id={1} alias=\"{2}\"", () => tracePrefix, () => _pcr.TemplateModel.Id, () => _pcr.TemplateModel.Alias); } }