如何检查Sitecore的项目是否使用别名 [英] How to check whether Sitecore item is using alias
问题描述
目前一个别名,在Sitecore的会产生多条路能SEO在某些情况下产生负面影响的内容相同的项目。
我要寻找一种方式来编程检查当前页/项目/ URL /请求是否使用别名或没有。
我希望会有线沿线的东西:
Sitecore.Web.WebUtil.IsAlias
这是如何检查别名的任何想法?
------- -------更新
下面是这似乎就好了我的工作,目前的解决方案......除非有人有更好的想法?
保护布尔IsAlias
{
得到
{
字符串FULLPATH = LinkManager.GetItemUrl(Sitecore.Context.Item);
返回HttpContext.Current.Request.RawUrl.StartsWith(FULLPATH,StringComparison.OrdinalIgnoreCase)!;
}
}
------更新2 ------
下面是根据严的建议,一个可行的解决方案。我不相信Sitecore.Context.Database.Aliases.GetTargetUrl()正在为Sitecore的6.4.1。所以只好凑合了一点。
如果(Sitecore.Configuration.Settings.AliasesActive&安培;&安培;
Sitecore.Context.Database.Aliases.Exists(HttpContext.Current.Request.RawUrl))
{
常量字符串格式=<链接相对= \\规范\\的href = \\{0}:// {1} {2} \\/>中;
项目targetItem = Sitecore.Context.Database.GetItem(Sitecore.Context.Database.Aliases.GetTargetID(HttpContext.Current.Request.RawUrl));
返回的String.Format(格式,HttpProtocol,HttpContext.Current.Request.Url.Host,LinkManager.GetItemUrl(targetItem));
}
我靠 Context.Database.Aliases.Exists(路径)
你的情况。此外,这似乎是一个好主意,检查是否别名在web.config活跃: Sitecore.Configuration.Settings.AliasesActive
Currently an "Alias" in Sitecore will produce multiple routes to the same content item which can negatively affect SEO in some cases.
I am looking for a way to programatically check whether the current Page/Item/URL/Request is using an alias or not.
I was hoping there would be something along the lines of:
Sitecore.Web.WebUtil.IsAlias
Any ideas on how to check for aliases?
-------UPDATE-------
Here is my current solution which appears to work just fine... Unless anyone has a better ideas?:
protected bool IsAlias
{
get
{
string fullPath = LinkManager.GetItemUrl(Sitecore.Context.Item);
return !HttpContext.Current.Request.RawUrl.StartsWith(fullPath, StringComparison.OrdinalIgnoreCase);
}
}
------ UPDATE 2 ------
Here is a working solution based on Yan's suggestions. I don't believe Sitecore.Context.Database.Aliases.GetTargetUrl() is working as of Sitecore 6.4.1. so I had to improvise a little.
if (Sitecore.Configuration.Settings.AliasesActive &&
Sitecore.Context.Database.Aliases.Exists(HttpContext.Current.Request.RawUrl))
{
const string format = "<link rel=\"canonical\" href=\"{0}://{1}{2}\"/>";
Item targetItem = Sitecore.Context.Database.GetItem(Sitecore.Context.Database.Aliases.GetTargetID(HttpContext.Current.Request.RawUrl));
return String.Format(format, HttpProtocol, HttpContext.Current.Request.Url.Host, LinkManager.GetItemUrl(targetItem));
}
I would rely on the Context.Database.Aliases.Exists(path)
in your case. Also, it seems to be a good idea to check whether the aliases are active in the web.config: Sitecore.Configuration.Settings.AliasesActive
.
这篇关于如何检查Sitecore的项目是否使用别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!