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); } } } |


