假设我有一个ASP.NET Web窗体网站,我有一个页面化的网格视图。在GridView中,有指向网站中其他页面的链接,这些链接可能是整个网站中指向此内容的唯一链接。目前,谷歌和其他搜索引擎可能只能跟踪第一页上出现的链接,因为GridView寻呼机生成的链接如下:
<a href="javascript:__doPostBack('GridTest','Page$2')">2</a>
因此,谷歌将永远无法加载其他网页来爬行链接。
我需要一个快速和肮脏的方式,谷歌索引所有可能隐藏在这些javascript链接下的页面。
我已经考虑过在CSS中创建一个可见性关闭的链接,该链接将加载网格视图,其中所有记录都可见,没有分页。这是一个很好的解决方法吗?
如果我有这个隐藏链接,我怎么能阻止搜索引擎索引那个页面(因为我不希望普通访问者访问它),但仍然跟踪和索引它链接到的页面。
有人有什么想法吗?谢谢你的帮助。
我喜欢科林的主意。我已经在使用自定义寻呼机控件,因此可以控制寻呼链接。为了实现他的建议,我为链接按钮创建了一个控件适配器,如果您给它一个,它将允许它呈现href属性,并将回发的javascript放在onclick中(但是它使它成为这样,您就不能同时使用onclientclick属性)。
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.Adapters;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.Adapters;
/// <summary>
/// This adapter allows us to specify the href attribute and have it rendered on the page.
/// When the href is specified the postback javascript will go into the onclick. The javascript will
/// cancel the href so it will post back like normal , but for people without javascript
/// the link should work, and search crawlers should be able to index the links
///
/// Styles that you can set like Font-Bold and Font-Underline are not going to work
/// you will need to use the style attribute, or set it in css
///
/// </summary>
public class LinkButtonAdapter : WebControlAdapter
{
protected override void Render(HtmlTextWriter writer)
{
LinkButton linkButton = (LinkButton)Control;
if (linkButton != null)
{
writer.WriteBeginTag("a");
writer.WriteAttribute("id", linkButton.ClientID);
if (!string.IsNullOrEmpty(linkButton.ToolTip))
{
writer.WriteAttribute("title", linkButton.ToolTip);
}
if (!string.IsNullOrEmpty(linkButton.CssClass))
{
writer.WriteAttribute("class", linkButton.CssClass);
}
if (!string.IsNullOrEmpty(linkButton.Attributes["style"]))
{
writer.WriteAttribute("style", linkButton.Attributes["style"]);
}
if (linkButton.Enabled)
{
if (!string.IsNullOrEmpty(linkButton.Attributes["href"]))
{
//if the user has set the href render it, and render the javascript in the onclick
//this will negate the client click script so be careful
writer.WriteAttribute("href", linkButton.Attributes["href"]);
string ClientScript = Page.ClientScript.GetPostBackClientHyperlink(linkButton, "");
if (!string.IsNullOrEmpty(ClientScript))
{
writer.WriteAttribute("onclick", ClientScript + ";return false;");
}
}
else
{
writer.WriteAttribute("href", Page.ClientScript.GetPostBackClientHyperlink(linkButton, ""));
if (!string.IsNullOrEmpty(linkButton.OnClientClick))
{
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, linkButton.OnClientClick);
}
}
}
else
{
writer.WriteAttribute("disabled", "disabled");
}
writer.Write(HtmlTextWriter.TagRightChar);
//apparently link buttons can contain other controls, who knew
//when they are databound, and you have the expression in between the
//tags it gets created as a LiteralControl
//the behavior for linkbuttons seems to be to overwrite whatever
//you set in the text propery with what is in between the begin and end tags
//
//Unless you have a databound control, then the text and the inner text seems
//to be concatenated together, which seems weird
//
//Also sometimes it generates literalcontrols, but removes them, and sometime it leave them
//but only if you have another non literal control inside
//
//I don't want to try to emulate this right now it is too confusing.
//Just don't use the text and the inner html at the same time, and I am leaving all controls
//that have been added
foreach (Control c in linkButton.Controls)
{
if (c is LiteralControl)
{
linkButton.Text = ((LiteralControl)c).Text;
}
else if (c is DataBoundLiteralControl)
{
linkButton.Text = ((DataBoundLiteralControl)c).Text;
}
else
{
c.RenderControl(writer);
}
}
writer.Write(linkButton.Text);
writer.WriteEndTag("a");
Page.ClientScript.RegisterForEventValidation(linkButton.UniqueID);
}
}
}
然后将其放入浏览器文件:
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.WebControls.LinkButton"
adapterType="LinkButtonAdapter" />
</controlAdapters>
</browser>
</browsers>