我有一个非常特殊的问题。对于某些特定的url nop网站重定向到另一个网址。例如 -
这是应该去的网址 -
点击或甚至粘贴在地址栏中时,我调试了代码,并在application_beginrequest
方法中注意了网址 -
(注意请求对象的路径属性)
它自动从category_whisky-american-sour-mash
更改为shipping-and-returns
。从类别和页面检查了两个SEO网址,但没有重复。
我查了SEO网址,只有一个条目,所以没有可能的重复方式
我现在试图解决这个问题差不多一天了。有人可以帮忙吗?为什么会这样?我对NopCommerce相对较新。任何建议都表示赞赏。
答案 0 :(得分:0)
好的,经过一天半的尝试,我终于找到了解决办法。我想我应该在这里发布解决方案。
这是由于错误的Slug
值造成的。在UrlRecordService
此方法中,如果没有找到slug,则通过放置""
来缓存slug -
public virtual string GetActiveSlug(int entityId, string entityName, int languageId)
{
if (_localizationSettings.LoadAllUrlRecordsOnStartup)
{
string key = string.Format(URLRECORD_ACTIVE_BY_ID_NAME_LANGUAGE_KEY, entityId, entityName, languageId);
return _cacheManager.Get(key, () =>
{
//load all records (we know they are cached)
var source = GetAllUrlRecordsCached();
var query = from ur in source
where ur.EntityId == entityId &&
ur.EntityName == entityName &&
ur.LanguageId == languageId &&
ur.IsActive
orderby ur.Id descending
select ur.Slug;
var slug = query.FirstOrDefault();
//little hack here. nulls aren't cacheable so set it to ""
if (slug == null)
slug = ""; //slug is blank for null
return slug;
});
}
else
{
//gradual loading
string key = string.Format(URLRECORD_ACTIVE_BY_ID_NAME_LANGUAGE_KEY, entityId, entityName, languageId);
return _cacheManager.Get(key, () =>
{
var source = _urlRecordRepository.Table;
var query = from ur in source
where ur.EntityId == entityId &&
ur.EntityName == entityName &&
ur.LanguageId == languageId &&
ur.IsActive
orderby ur.Id descending
select ur.Slug;
var slug = query.FirstOrDefault();
//little hack here. nulls aren't cacheable so set it to ""
if (slug == null)
slug = ""; // slug is blank for null
return slug;
});
}
}
最终创建了许多带有空白slu ur的网址。要解决此问题,您应该执行以下操作 -
然后添加检查以忽略带有isActive = False
-
public virtual UrlRecordForCaching GetBySlugCached(string slug)
{
if (String.IsNullOrEmpty(slug))
return null;
if (_localizationSettings.LoadAllUrlRecordsOnStartup)
{
//load all records (we know they are cached)
var source = GetAllUrlRecordsCached();
var query = from ur in source
where ur.Slug.Equals(slug, StringComparison.InvariantCultureIgnoreCase)
&& ur.IsActive // 2 add IsActive check
select ur;
var urlRecordForCaching = query.FirstOrDefault();
return urlRecordForCaching;
}
else
{
//gradual loading
string key = string.Format(URLRECORD_BY_SLUG_KEY, slug);
return _cacheManager.Get(key, () =>
{
if (string.IsNullOrEmpty(slug)) // 1 blank slug check
return null;
var urlRecord = GetBySlug(slug);
if (urlRecord == null)
return null;
var urlRecordForCaching = Map(urlRecord);
return urlRecordForCaching;
});
}
}
/// <summary>
/// Gets all URL records
/// </summary>
/// <param name="slug">Slug</param>
/// <param name="pageIndex">Page index</param>
/// <param name="pageSize">Page size</param>
/// <returns>Customer collection</returns>
public virtual IPagedList<UrlRecord> GetAllUrlRecords(string slug, int pageIndex, int pageSize)
{
var query = _urlRecordRepository.Table;
if (!String.IsNullOrWhiteSpace(slug))
query = query.Where(ur => ur.Slug.Contains(slug) && ur.IsActive); // 2 add IsActive check
query = query.OrderBy(ur => ur.Slug);
var urlRecords = new PagedList<UrlRecord>(query, pageIndex, pageSize);
return urlRecords;
}
/// <summary>
/// Find URL record
/// </summary>
/// <param name="slug">Slug</param>
/// <returns>Found URL record</returns>
public virtual UrlRecord GetBySlug(string slug)
{
if (String.IsNullOrEmpty(slug))
return null;
var query = from ur in _urlRecordRepository.Table
where ur.Slug == slug && ur.IsActive // 2 add IsActive check
select ur;
var urlRecord = query.FirstOrDefault();
return urlRecord;
}
protected virtual IList<UrlRecordForCaching> GetAllUrlRecordsCached()
{
//cache
string key = string.Format(URLRECORD_ALL_KEY);
return _cacheManager.Get(key, () =>
{
var query = from ur in _urlRecordRepository.Table
where ur.IsActive # 2 add IsActive check
select ur;
var urlRecords = query.ToList();
var list = new List<UrlRecordForCaching>();
foreach (var ur in urlRecords)
{
var urlRecordForCaching = Map(ur);
list.Add(urlRecordForCaching);
}
return list;
});
}
/// <summary>
/// Gets all cached URL records
/// </summary>
/// <returns>cached URL records</returns>
protected virtual IList<UrlRecordForCaching> GetAllUrlRecordsCached()
{
//cache
string key = string.Format(URLRECORD_ALL_KEY);
return _cacheManager.Get(key, () =>
{
var query = from ur in _urlRecordRepository.Table
where ur.IsActive // 2 add isActive check
select ur;
var urlRecords = query.ToList();
var list = new List<UrlRecordForCaching>();
foreach (var ur in urlRecords)
{
var urlRecordForCaching = Map(ur);
list.Add(urlRecordForCaching);
}
return list;
});
}
另外,请不要忘记清除Cookie并重新启动服务器以防万一。
所以,这远远解决了我的问题。