清空-青空-晴空

首页‎ > ‎技术‎ > ‎C# .NET‎ > ‎

404 page not found

发布者:Bowen Gu,发布时间:‎‎2009-10-12 上午7:11‎
http://helephant.com/2009/02/improving-the-way-aspnet-handles-404-requests/

        <customErrors mode="On">
            <error statusCode="404" redirect="404.aspx" />
        </customErrors>

        <httpModules>
            <add name="ErrorModule" type="ErrorModule, App_Code"/>
        </httpModules>

ErrorModule : IHttpModule

    public void Init(HttpApplication context)
    {
        context.Error += context_Error;
    }

    #endregion

    void context_Error(object sender, EventArgs e)
    {
        var context = HttpContext.Current;

        var error = context.Server.GetLastError() as HttpException;
        if (error.GetHttpCode() == 404)
        {

            var config =
                (CustomErrorsSection)WebConfigurationManager.GetSection("system.web/customErrors");
            if (config.Mode == CustomErrorsMode.On ||
                (config.Mode == CustomErrorsMode.RemoteOnly && context.Request.Url.Host != "localhost"))
            {
                context.Response.StatusCode = 404;
                //Server.ClearError();
                if (config.Errors["404"] != null)
                    HttpContext.Current.Server.Transfer(config.Errors["404"].Redirect);
                else
                    HttpContext.Current.Server.Transfer(config.DefaultRedirect);
            }
        }
    }



蛰伏是另一段旅程的开始,人生总有浮云蔽日时,若能善用时光,蓄劲待发,静待云轻日开,则生命的甘泉,终有奔涛流涌、万里清畅的一天。

MSN
   
MAIL
   
   

209 天前是
五一

229 天前是
太湖两日游