Skip to content

Commit 1b5830a

Browse files
V9: Visiting a page without a template returns "Page Not Found" (#11852)
* Returns "Page Not Found" when template doesn't exist for a docType * Fix comment * Use IsNullOrWhiteSpace instead of IsNullOrEmpty Co-authored-by: Mole <[email protected]>
1 parent 364b8f1 commit 1b5830a

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/Umbraco.Web.Common/Controllers/RenderController.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ protected IUmbracoContext UmbracoContext
5353
/// </summary>
5454
public virtual IActionResult Index() => CurrentTemplate(new ContentModel(CurrentPage));
5555

56+
/// <summary>
57+
/// Gets an action result based on the template name found in the route values and a model.
58+
/// </summary>
59+
/// <typeparam name="T">The type of the model.</typeparam>
60+
/// <param name="model">The model.</param>
61+
/// <returns>The action result.</returns>
62+
/// <remarks>
63+
/// If the template found in the route values doesn't physically exist, Umbraco not found result is returned.
64+
/// </remarks>
65+
protected override IActionResult CurrentTemplate<T>(T model)
66+
{
67+
if (EnsurePhsyicalViewExists(UmbracoRouteValues.TemplateName) == false)
68+
{
69+
// no physical template file was found
70+
return new PublishedContentNotFoundResult(UmbracoContext);
71+
}
72+
73+
return View(UmbracoRouteValues.TemplateName, model);
74+
}
75+
5676
/// <summary>
5777
/// Before the controller executes we will handle redirects and not founds
5878
/// </summary>
@@ -123,6 +143,6 @@ private PublishedContentNotFoundResult GetNoTemplateResult(IPublishedRequest pcr
123143
{
124144
return new PublishedContentNotFoundResult(UmbracoContext);
125145
}
126-
}
146+
}
127147
}
128148
}

src/Umbraco.Web.Common/Controllers/UmbracoPageController.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected virtual IPublishedContent CurrentPage
7474
/// <param name="model">The model.</param>
7575
/// <returns>The action result.</returns>
7676
/// <exception cref="InvalidOperationException">If the template found in the route values doesn't physically exist and exception is thrown</exception>
77-
protected IActionResult CurrentTemplate<T>(T model)
77+
protected virtual IActionResult CurrentTemplate<T>(T model)
7878
{
7979
if (EnsurePhsyicalViewExists(UmbracoRouteValues.TemplateName) == false)
8080
{
@@ -90,6 +90,13 @@ protected IActionResult CurrentTemplate<T>(T model)
9090
/// <param name="template">The view name.</param>
9191
protected bool EnsurePhsyicalViewExists(string template)
9292
{
93+
if (string.IsNullOrWhiteSpace(template))
94+
{
95+
string docTypeAlias = UmbracoRouteValues.PublishedRequest.PublishedContent.ContentType.Alias;
96+
_logger.LogWarning("No physical template file was found for document type with alias {Alias}", docTypeAlias);
97+
return false;
98+
}
99+
93100
ViewEngineResult result = _compositeViewEngine.FindView(ControllerContext, template, false);
94101
if (result.View != null)
95102
{
@@ -99,6 +106,5 @@ protected bool EnsurePhsyicalViewExists(string template)
99106
_logger.LogWarning("No physical template file was found for template {Template}", template);
100107
return false;
101108
}
102-
103109
}
104110
}

0 commit comments

Comments
 (0)