【问题标题】:NopCommerce append numeric on an existing slugNopCommerce 在现有的 slug 上附加数字
【发布时间】:2015-07-03 07:47:16
【问题描述】:

在 NopCommerce 中,如果这样的 slug 已经存在(添加或更新),则会添加一个 slug 的增量值。现有的 slug "product-name-here" 变为 "product-name-here-1"。如果我添加相同的 slug,那么它现在变成“product-name-here-2”。

我似乎无法在“UrlRecordService.cs”文件中找到处理在每个 slug 末尾附加数值的文件。

非常感谢任何帮助。

【问题讨论】:

    标签: c# nopcommerce slug


    【解决方案1】:

    它在SeoExtensions 类中的ValidateSeName 扩展方法中实现。

        public static string ValidateSeName<T>(this T entity, string seName, string name, bool ensureNotEmpty)
             where T : BaseEntity, ISlugSupported
        {
            if (entity == null)
                throw new ArgumentNullException("entity");
    
            //use name if sename is not specified
            if (String.IsNullOrWhiteSpace(seName) && !String.IsNullOrWhiteSpace(name))
                seName = name;
    
            //validation
            seName = GetSeName(seName);
    
            //max length
            //For long URLs we can get the following error:
            //"the specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters"
            //that's why we limit it to 200 here (consider a store URL + probably added {0}-{1} below)
            seName = CommonHelper.EnsureMaximumLength(seName, 200);
    
            if (String.IsNullOrWhiteSpace(seName))
            {
                if (ensureNotEmpty)
                {
                    //use entity identifier as sename if empty
                    seName = entity.Id.ToString();
                }
                else
                {
                    //return. no need for further processing
                    return seName;
                }
            }
    
            //ensure this sename is not reserved yet
            string entityName = typeof(T).Name;
            var urlRecordService = EngineContext.Current.Resolve<IUrlRecordService>();
            var seoSettings = EngineContext.Current.Resolve<SeoSettings>();
            int i = 2;
            var tempSeName = seName;
            while (true)
            {
                //check whether such slug already exists (and that is not the current entity)
                var urlRecord = urlRecordService.GetBySlug(tempSeName);
                var reserved1 = urlRecord != null && !(urlRecord.EntityId == entity.Id && urlRecord.EntityName.Equals(entityName, StringComparison.InvariantCultureIgnoreCase));
                //and it's not in the list of reserved slugs
                var reserved2 = seoSettings.ReservedUrlRecordSlugs.Contains(tempSeName, StringComparer.InvariantCultureIgnoreCase);
                if (!reserved1 && !reserved2)
                    break;
    
                tempSeName = string.Format("{0}-{1}", seName, i);
                i++;
            }
            seName = tempSeName;
    
            return seName;
        }
    

    【讨论】:

    • 谢谢,你找到了!
    猜你喜欢
    • 1970-01-01
    • 2013-09-19
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 2016-12-06
    相关资源
    最近更新 更多