【问题标题】:Problem with UrlRewriting in NopCommerceNopCommerce 中的 UrlRewriting 问题
【发布时间】:2011-05-19 18:16:48
【问题描述】:

我尝试在 NopCommerce 中进行更改以在地址栏中包含语言我知道似乎有什么问题。

当我禁用 UrlRewriting 时一切正常,当我启用它时,当我使用默认语言时,当我转到另一种非默认语言时一切正常,我遇到了问题。

我有两部分代码用于默认语言和其他语言

我修改了一点代码,所以现在主函数可以在语言之间进行选择:

    public static string GetCategoryUrl(Category category, int languageId)
    {
        if (category == null)
            throw new ArgumentNullException("category");
        string seName = GetSEName(category.SEName);

        if (String.IsNullOrEmpty(seName))
        {
            var categoryLocalized = CategoryManager.GetCategoryLocalizedByCategoryIdAndLanguageId(category.CategoryId, languageId);
            if (categoryLocalized != null)
            {
                seName = GetSEName(categoryLocalized.Name);
            }
            else
            {
            seName = GetSEName(category.Name);
            }
        }            

        int defaultLanguage = Convert.ToInt32(SettingManager.GetSettingValue("Localization.DefaultLanguageID"));
        string url = String.Empty;

        string url2 = String.Empty;

        //***for default language***
        if (languageId == defaultLanguage)
        {
            url2 = SEOHelper.EnableUrlRewriting ? SettingManager.GetSettingValue("SEO.Category.UrlRewriteFormat") : "{0}Category.aspx?CategoryID={1}";
            url = string.Format(url2, CommonHelper.GetStoreLocation(), category.CategoryId, seName);
        }

        //***for other languages***
        else
        {
            url2 = SEOHelper.EnableUrlRewriting ? SettingManager.GetSettingValue("SEO.Category.UrlRewriteFormat2") : "{0}Category.aspx?Language={1}&CategoryID={2}";
            url = string.Format(url2, CommonHelper.GetStoreLocation(), GetLocaleSubFolder(languageId), category.CategoryId, seName);
        }
        return url.ToLowerInvariant();
    }

我也有默认语言: 对于 SEO.Category.UrlRewriteFormat 我在数据库中有默认语言:{0}c{1}/{2}

在 UrlRewriting.config 中,我对默​​认语言有以下规则:

没有 url 重写我上面的链接看起来像 www.nopcomerce.com/category.aspx?categoryid=10

当我转到默认语言的类别时,我的链接看起来像 www.nopcomerce.com/c10/somecategory

对于其他语言:

对于 SEO.Category.UrlRewriteFormat2,我有其他语言的数据库:{0}{1}/c{2}/{3}

对于其他语言,我有

没有 url 重写为其他语言的链接看起来像 www.nopcomerce.com/category.aspx?language=de&categoryid=10

例如,当我去德语的同一类别时,我会 www.nopcomerce.com/de/c10/somecategorylocalizedingerman

现在我知道该页面可以正常工作,正如我之前所说,因为当我在 NopCommerce 中禁用 UrlRewriting 时,所有语言的所有页面都可以正常工作。我可以在类别、产品和整个门户之间更改语言,每种语言都没有问题。

但是当我启用 UrlRewriting 时,默认语言的类别链接可以正常工作 (www.nopcomerce.com/c10/somecategory),但是当我点击链接时在其他语言中,每次我单击任何链接时,例如其他语言的类别链接,显示的内容来自默认页面(就像它在那里重定向我一样)但我看到我想要进入的链接语言写在地址栏中 (www.nopcomerce.com/de/c10/somecategorylocalizedingerman)。

我尝试了所有方法,但我知道问题出在哪里。怎么了?

我也尝试在 NopCommerce 论坛寻求帮助,但那里没有任何帮助。

你可以阅读我开始写的这个问题,直到这部分我现在不知道什么似乎是一个问题。

http://www.nopcommerce.com/boards/t/1039/seo-and-multilingual-pages.aspx?p=1

提前感谢您的每一个帮助。

【问题讨论】:

    标签: .net nopcommerce


    【解决方案1】:

    经过几天的折磨和几个小时我放弃了一件事,那就是链接会如我所愿。

    除了默认链接之外,我从来没有设法为语言做这些链接,看起来像:

    nopcommerce.com/国家/地区/类别/类别名称

    我设法实现的最接近的方法是链接我的外观

    nopcommerce.com / 类别 / 国家 / 类别名称。

    我还设法制作了我们不是经典 ImageButton 的链接,而不是常规的超链接和我偶然发现的一个有趣问题的链接。

    NopCommerce 使用 cookie 更改语言。

    我找到了 cookie 的名称,并在他将其写入 NopContext.Current.WorkingLanguage 时。

    以类似的方式,我也创建了一个javascript函数setCookie()

    function setCookie(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) +
            ((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
    }
    

    在 cookie 中创建所选语言的日志值,并在用户单击所选语言的标志时激活。

    例如

    string coockie = String.Format("javascript:setCookie('{0}','{1}','{2}');", "Nop.CustomerLanguage", language.LanguageId.ToString(), new TimeSpan(365, 0, 0, 0, 0));
    hpLanguage.Attributes.Add("onclick", coockie);
    

    现在我们来到了问题发生的有趣部分,我不清楚它为什么会发生。

    假设我们有德语和英语

    当我第一次点击德语的标志时,在地址栏中显示一个指向德语的链接,但内容仍然是英语。只有当第二次(再次)我点击德语中的标志时,页面的内容才切换到德语。

    我不明白为什么这会发生在我身上?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      相关资源
      最近更新 更多