比CustomErrors更好asp.net處理的方案

原問題:

404_vs_302_ASP_Net_Custom_Errors.aspx

 

解決方案:

In these days I've been experiencing a conflict between the ASP.Net architecture and the rules that search engines gurus advice to follow... I introduce you to the problem:

  • Scenario: well positioned - in terms of search engines - website needs to change domain.
  • Goal: we have to try as hard as we can to mantain pages' rankings gained among the various search engines.
  • Remarks: all the pages are .aspx web forms.

We got informed that the search engines - let's say Google ...ok? - prefers to get a 404 response and some extra information about a possible alternative link...

Here's the ASP.Net vs Search Engines idiosyncrasy! If you handle the 404 exception the "usual way" (using customErrors section in your web.config) you get an undesired effect:
Let's say that the 404 page you set forces a 404 status code like that...

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    void Page_Load(object o, EventArgs e)
    {
        Response.StatusCode = 404;
        lbl.Text = Response.Status;
    }
    
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>404 - Not Found</title>
</head>
<body>
        <asp:Image runat="server" ID="img" ImageUrl="~/Images/Icons/ico_warning.gif" AlternateText="404" ImageAlign="Middle" />
        <asp:Label runat="server" ID="lbl" Font-Bold="true" />
    
</body>
</html>

What you'll get is first a 302 status code (that means "redirect") and then the 404 one! (you can test it using Fiddler)

* That is to avoid in order to please search engines! *

Here it comes my solution...
The idea is to intercept a 404 HttpException via HttpModule and return the content of the 404 page written above.

Here's the IHttpModule implementation code:

using System;
using System.Web;

namespace PacemWebSite.BLL.Web
{
    class PacemHttpModule : System.Web.IHttpModule
    {
        public void Dispose()
        {
            //...
        }

        public void Init(HttpApplication context)
        {
            // putting a listener to the error event:                    
            context.Error += new EventHandler(context_Error);
        }

        void context_Error(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            Exception lastError = app.Server.GetLastError();    
            HttpException he = lastError as HttpException;        
            // handling only HttpExceptions (and 404 in particular):
            if (he != null)
            {
                int httpErrorCode = he.GetHttpCode();
                if (httpErrorCode == 404)
                {   
                    // clearing the error in order to avoid the exception to be thrown:
                    app.Server.ClearError();
                    string fofPage = "~/Pages/404.aspx";
                    // setting/changing the handler for the current response:
                    app.Context.Handler = System.Web.UI.PageParser.GetCompiledPageInstance(fofPage, app.Server.MapPath(fofPage), app.Context);
                }
            }
        }
    }
}

Obviously, you must add your custom HttpModule to your Web.config as usual:

<httpModules>
    <add name="FourOhFourModule" 
    type="PacemWebSite.BLL.Web.PacemHttpModule, PacemWebSite"/>
</httpModules> 

Now the response will be search engines pleasant with its straight 404!...

404 response

Take care. Bye.


Direct Link to this Article

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值