Cynical Developer
07/27/2024, 5:40 PM'Accounts'
in a multilingual size, and I've made a custom controller. Now, when you go to /en-us/accounts/
it hits the below the custom controller.
but I also want to hit the controller for /en-us/accounts/{some name}
with it being multilingual the route isn't always account, as an example in a french translation it would be /fr/comptes/
and /fr/comptes/{some name}
How do I intercept the call to the child routes, and have I even done this correctly, as just because it works doesn't means it right? 😄
here is the custom controller:
csharp
public class AccountsController(
IUmbracoContextAccessor umbracoContextAccessor,
ILogger<RenderController> logger,
ICompositeViewEngine compositeViewEngine) : RenderController(logger, compositeViewEngine, umbracoContextAccessor)
{
public override IActionResult Index()
{
var accountPage = CurrentPage as Accounts;
if (accountPage == null)
{
return NotFound();
}
//some custom logic to do some stuff here
return CurrentTemplate(new ContentModel(accountPage));
}
}
Matt Wise
07/27/2024, 6:58 PMCynical Developer
07/27/2024, 7:09 PMMatt Wise
07/27/2024, 8:26 PMMatt Wise
07/27/2024, 8:26 PMSander L
07/28/2024, 2:52 PMCynical Developer
07/28/2024, 3:18 PMMike Chambers
07/28/2024, 9:20 PMIVirtualPageController
approach https://docs.umbraco.com/umbraco-cms/v/13.latest-lts/reference/routing/custom-routes#custom-route-with-ivirtualpagecontrollerCynical Developer
07/29/2024, 7:27 AMMike Chambers
07/29/2024, 8:52 AMcsharp
Endpoints = app => app.UseEndpoints(endpoints =>
{
IEnumerable<ILanguage> languages = _localizationService.GetAllLanguages();
var accountsNode = umbracoContext.Content.GetById({GUID});
foreach (ILanguage language in languages)
{
CultureInfo cultureInfo = language.CultureInfo;
var accountsNodeCulturalName = accountsNode.Name(cultureInfo.IsoCode);
endpoints.MapControllerRoute(
"Accounts Controller",
$"/{accountsNodeCulturalName}/{{action}}/{{id?}}",
new {Controller = "Accounts", Action = "Index"});
}
})
Actually you might want accountsNode.Url(cultureInfo.IsoCode)
to construct the endpoints.. (and I'm assuming here that you multilanguage setup is using language variants, and not a duplicate node tree)Matt Wise
07/29/2024, 9:02 AMMatt Wise
07/29/2024, 9:08 AMCynical Developer
07/29/2024, 9:41 AMCynical Developer
07/29/2024, 10:00 AMMatt Wise
07/29/2024, 10:01 AMMatt Wise
07/29/2024, 10:02 AMbuilder.ContentFinders().InsertBefore<ContentFinderByUrl, YourFinder>();
in a composerMatt Wise
07/29/2024, 10:02 AMMatt Wise
07/29/2024, 10:02 AMCynical Developer
07/29/2024, 10:29 AMMatt Wise
07/29/2024, 10:30 AMMatt Wise
07/29/2024, 10:30 AMCynical Developer
07/29/2024, 10:31 AMCynical Developer
07/29/2024, 10:32 AMcsharp
public class AccountFinder : IContentFinder
{
private readonly IAppPolicyCache _runtimeCache;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly IShortStringHelper _shortStringHelper;
public AccountFinder(AppCaches appCaches,
IUmbracoContextAccessor umbracoContextAccessor,
IShortStringHelper shortStringHelper)
{
_runtimeCache = appCaches.RuntimeCache;
_umbracoContextAccessor = umbracoContextAccessor;
_shortStringHelper = shortStringHelper;
}
public Task<bool> TryFindContent(IPublishedRequestBuilder request)
{
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
{
return Task.FromResult(false);
}
if (request.Domain?.Name is not null)
{
var countryIndex = request.Uri.ToString().IndexOf(request.Domain?.Name!, StringComparison.OrdinalIgnoreCase);
if (countryIndex < 0)
{
return Task.FromResult(false);
}
}
var route = HttpUtility.UrlDecode(string.Concat(request.Uri.Segments.SkipLast(1).TakeLast(1)));
route = request.Domain!.ContentId.ToString("0/") + route;
var accountPage = umbracoContext.Content?.GetByRoute(route);
if (accountPage is { ContentType.Alias: Accounts.ModelTypeAlias })
{
var displayName = string.Concat(request.Uri.Segments.TakeLast(1));
request.SetPublishedContent(accountPage);
request.SetTemplate(new Template(_shortStringHelper, "Account", "account"));
return Task.FromResult(true);
}
else
{
return Task.FromResult(false);
}
}
}
Matt Wise
07/29/2024, 10:33 AMA hub and casual space for you to interact with fellow community members and learn more about Umbraco!
Powered by