【问题标题】:Force culture/multilang page when requesting non-culture page请求非文化页面时强制文化/多语言页面
【发布时间】:2017-04-27 22:52:52
【问题描述】:

在我的 Umbraco 网站上,如果有人尝试访问没有多语言部分的页面,是否可以强制重定向到所有页面的默认多语言页面?

这是一个例子:

website.com/en/news
website.com/da/news

如果有人试图访问:

website.com/news

它应该重定向到默认的文化页面:

website.com/en/news

我猜这样的事情应该很容易(因为我认为这是大多数用户想要的),但我不知道怎么做。

【问题讨论】:

  • /news 是真实页面,还是 /en 和 /da 下的所有网站内容?
  • @Tim 所有页面都在文化之下。
  • 另一个快速的问题,您当前的解决方案代码在哪里运行?是 ContentFinder 还是其他?
  • @Tim 嗯?对不起,我不明白。
  • 你下面的解决方案,你在哪里运行该代码?

标签: asp.net umbraco umbraco7


【解决方案1】:

所以我最终做了一个糟糕的修复,直到我得到一个更好的“答案”。它是这样的:

var culture = HttpContext.Current.Request.Url.AbsolutePath.Split('/')[1];
if (culture != "en" && culture != "da")
{
    var shortCulture = "en";

    var languages = HttpContext.Current.Request.UserLanguages;

    //Just making sure
    if (languages != null && languages.Length > 0)
    {
        shortCulture = languages[0].Trim().Substring(0, 2);

        //If the culture is something like fr (French), we default back to en
        if (shortCulture != "en" && shortCulture != "da")
        {
            shortCulture = "en";
        }
    }
    Uri uri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri);
    var url = uri.Scheme + Uri.SchemeDelimiter + uri.Host;
    url += "/" + shortCulture + HttpContext.Current.Request.Url.AbsolutePath;
    Response.RedirectPermanent(url);
}

所以基本上,如果我尝试请求:

https://website.com/news
http://website.com/news
website.com/news
http://website.com/news/some-news-article/
etc. etc.

它将重定向。示例:

https://website.com/news/
|
V
https://website.com/en/news/

(这取决于您的语言环境)。

可怕,可怕,可怕。我很惭愧。

【讨论】:

    【解决方案2】:

    好的,所以在 Umbraco 7 中,您可以使用 ContentFinder 执行此操作。这是作为内容管道的一部分运行的东西,它将尝试查找 Umbraco 自身无法找到的内容。

    内容查找器有两个部分,即实际的 ContentFinder 本身,然后您必须将其连接到 Umbraco 的应用程序启动事件中。

    我实际上做了一些与您正在尝试的事情类似的事情,但相反(我的所有内容都在 /content 中,但我使用 /en-en/content 之类的虚拟 URL 并将所有内容映射回内容查找器中)。

    有关 ContentFinder 的信息,请查看文档:https://our.umbraco.org/documentation/reference/routing/request-pipeline/icontentfinder

    这是我发现的一个要点,它几乎可以满足您的需求:https://gist.github.com/alindgren/4f8e47d9b2d769137be3

    ContentFinder 的优势在于它只会针对未找到的内容运行,而不是像您现在拥有的解决方案那样在每次页面加载时运行,从而提高效率!

    希望有帮助!

    :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      相关资源
      最近更新 更多