Web.Routing - expose PublishedContentRequest.Template as a string

This commit is contained in:
Stephan
2013-01-24 08:51:27 -01:00
parent b3e3855931
commit 197ab6357f
2 changed files with 89 additions and 39 deletions

View File

@@ -56,7 +56,7 @@ namespace Umbraco.Web.Routing
_engine = new PublishedContentRequestEngine(this);
this.RenderingEngine = RenderingEngine.Mvc; // default
this.RenderingEngine = RenderingEngine.Unknown;
}
/// <summary>
@@ -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
/// <summary>
/// Gets or sets the template to use to display the requested content.
/// <summary>
/// The template model, if any, else <c>null</c>.
/// </summary>
public Template Template { get; set; }
//private Umbraco.Core.Models.ITemplate _templateModel;
private Template _template;
/// <summary>
/// Gets or sets the template model to use to display the requested content.
/// </summary>
// 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;
}
}
/// <summary>
/// Gets the template to use to display the requested content.
/// </summary>
public string Template
{
get
{
return _template == null ? null : _template.Alias;
}
}
/// <summary>
/// Tries to set the template to use to display the requested content.
/// </summary>
/// <param name="value">The name of the template.</param>
/// <returns>A value indicating whether a valid template with the specified name was found.</returns>
/// <remarks>Setting the template resets <c>RenderingEngine</c> to <c>Unknown</c>.</remarks>
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;
}
}
}
/// <summary>
/// Gets a value indicating whether the content request has a template.
/// </summary>
public bool HasTemplate
{
get { return this.Template != null; }
get { return _template != null; }
}
#endregion

View File

@@ -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
}
}
/// <summary>
/// Finds the rendering engine to use, and updates the PublishedContentRequest accordingly.
/// </summary>
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<PublishedContentRequestEngine>("{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<PublishedContentRequestEngine>("{0}Got template id={1} alias=\"{2}\"", () => tracePrefix, () => template.Id, () => template.Alias);
}
else
@@ -628,7 +603,7 @@ namespace Umbraco.Web.Routing
}
else
{
LogHelper.Debug<PublishedContentRequestEngine>("{0}Running with template id={1} alias=\"{2}\"", () => tracePrefix, () => _pcr.Template.Id, () => _pcr.Template.Alias);
LogHelper.Debug<PublishedContentRequestEngine>("{0}Running with template id={1} alias=\"{2}\"", () => tracePrefix, () => _pcr.TemplateModel.Id, () => _pcr.TemplateModel.Alias);
}
}