笑看山河

导航

 

今天SEO说,我们的自定义跳转错误页并不友好,因为在使用redirec时,先返回了302,然后才跳转到404自定义页面上去的,被百度视为异常跳转。为了解决这个小问题,网上查了下,看到这篇文章tp://www.cnblogs.com/death029/archive/2011/07/29/2120889.html

看后感觉虽然是解决了问题,但并不是我想要的,因为我需要在页面中控制判断信息是否存在。那么我的解决方案:

1:错误页代码:

protected void Page_Load(object sender, EventArgs e)
{
    Response.StatusCode = 404;
    string msg = Requests.GetQueryStringString("msg");
    if (!string.IsNullOrEmpty(msg))
    {
        this.ltlContent.Text = this.Server.UrlDecode(msg);
    }
    else
    {
        this.ltlContent.Text = Config.ErrorProInfo;
    }
}

2:通用方法代码

/// <summary>
/// 获取并显示友好错误页信息(在当前页显示错误信息,友好SEO)
/// </summary>
/// <param name="message">错误信息</param>
/// <param name="page">当前页对象</param>
public static void ShowError(System.Web.UI.Page page, string message)
{
 page.Server.Execute(string.Format("/common/error.aspx?msg={0}", HttpUtility.UrlEncode(message, Encoding.UTF8)), true);
 page.Response.End();
}

3:页面调用代码:

if(true)
{
    xxxxx;
}
else
{
    Errors.ShowError(this.Page, "对不起!你所查找的数据不存在或已删除!");
}

主要用到了Server.Execute方法来加载指定错误页面内容,并在当前页显示,避免了进行多次请求而导致的302-》404的异常跳转;

分享一下,欢迎拍砖!

posted on 2012-12-05 18:19  笑看山河  阅读(1888)  评论(0编辑  收藏  举报