Merge pull request #1246 from umbraco/temp-u4-2508

U4-2508 - improve culture detection in 404 situation
This commit is contained in:
Claus
2016-05-03 11:02:31 +02:00
2 changed files with 45 additions and 36 deletions

View File

@@ -1,5 +1,7 @@
using System.Linq;
using System.Globalization;
using System.Linq;
using System.Web;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
@@ -20,15 +22,39 @@ namespace Umbraco.Web.Routing
{
LogHelper.Debug<ContentFinderByLegacy404>("Looking for a page to handle 404.");
// try to find a culture as best as we can
var errorCulture = CultureInfo.CurrentUICulture;
if (pcr.HasDomain)
{
errorCulture = CultureInfo.GetCultureInfo(pcr.UmbracoDomain.LanguageIsoCode);
}
else
{
var route = pcr.Uri.GetAbsolutePathDecoded();
var pos = route.LastIndexOf('/');
IPublishedContent node = null;
while (pos > 1)
{
route = route.Substring(0, pos);
node = pcr.RoutingContext.UmbracoContext.ContentCache.GetByRoute(route);
if (node != null) break;
pos = route.LastIndexOf('/');
}
if (node != null)
{
var d = DomainHelper.FindWildcardDomainInPath(pcr.RoutingContext.UmbracoContext.Application.Services.DomainService.GetAll(true), node.Path, null);
if (d != null && string.IsNullOrWhiteSpace(d.LanguageIsoCode) == false)
errorCulture = CultureInfo.GetCultureInfo(d.LanguageIsoCode);
}
}
// TODO - replace the whole logic
var error404 = NotFoundHandlerHelper.GetCurrentNotFoundPageId(
//TODO: The IContentSection should be ctor injected into this class in v8!
UmbracoConfig.For.UmbracoSettings().Content.Error404Collection.ToArray(),
//TODO: Is there a better way to extract this value? at least we're not relying on singletons here though
pcr.RoutingContext.UmbracoContext.HttpContext.Request.ServerVariables["SERVER_NAME"],
pcr.RoutingContext.UmbracoContext.Application.Services.EntityService,
new PublishedContentQuery(pcr.RoutingContext.UmbracoContext.ContentCache, pcr.RoutingContext.UmbracoContext.MediaCache),
pcr.RoutingContext.UmbracoContext.Application.Services.DomainService);
errorCulture);
IPublishedContent content = null;

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Xml;
@@ -232,47 +233,29 @@ namespace Umbraco.Web.Routing
/// <param name="domainService"></param>
/// <returns></returns>
internal static int? GetCurrentNotFoundPageId(
IContentErrorPage[] error404Collection,
string requestServerName,
IContentErrorPage[] error404Collection,
string requestServerName,
IEntityService entityService,
ITypedPublishedContentQuery publishedContentQuery,
IDomainService domainService)
{
if (error404Collection.Count() > 1)
throw new NotImplementedException();
}
internal static int? GetCurrentNotFoundPageId(
IContentErrorPage[] error404Collection,
IEntityService entityService,
ITypedPublishedContentQuery publishedContentQuery,
CultureInfo errorCulture)
{
if (error404Collection.Length > 1)
{
// try to get the 404 based on current culture (via domain)
IContentErrorPage cultureErr;
var d = domainService.GetByName(requestServerName);
if (d != null && d.LanguageId.HasValue)
{
// test if a 404 page exists with current culture
cultureErr = error404Collection
.FirstOrDefault(x => x.Culture == d.LanguageIsoCode);
if (cultureErr != null)
{
return GetContentIdFromErrorPageConfig(cultureErr, entityService, publishedContentQuery);
}
}
// test if a 404 page exists with current culture thread
cultureErr = error404Collection
.FirstOrDefault(x => x.Culture == System.Threading.Thread.CurrentThread.CurrentUICulture.Name);
if (cultureErr != null)
{
return GetContentIdFromErrorPageConfig(cultureErr, entityService, publishedContentQuery);
}
// there should be a default one!
cultureErr = error404Collection
.FirstOrDefault(x => x.Culture == "default");
var cultureErr = error404Collection.FirstOrDefault(x => x.Culture == errorCulture.Name)
?? error404Collection.FirstOrDefault(x => x.Culture == "default"); // there should be a default one!
if (cultureErr != null)
{
return GetContentIdFromErrorPageConfig(cultureErr, entityService, publishedContentQuery);
}
}
else
{