[Log]ASP.NET之HttpModule拦截404异常

Httpmodule代码:
public class Error404Module : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.Error += new EventHandler(Context_Error);
        }

        protected void Context_Error(object sender, EventArgs e)
        {
            HttpContext ctx = HttpContext.Current;
            HttpResponse response = ctx.Response;
            HttpRequest request = ctx.Request;

            Exception ex = ctx.Server.GetLastError();
            HttpException hex = ex as HttpException;
            if (hex != null && hex.GetHttpCode() == 404)
            {
                ctx.Server.ClearError();
                string url = request.Url.AbsoluteUri.ToLower();
                if (url.Contains(@"http://hj.woniu.com"))
                {
                    url = url.Replace(@"http://hj.woniu.com", @"http://sg.hj.woniu.com");
                }
                response.Redirect(url, true);
            }
        }
        public void Dispose()
        {

        }
    } 

Web.Config配置:

      "Error404Module" type="ActHJ.ErrorModules.Error404Module"/>
    



    "false">
      
        "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        "System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        "System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        "System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
      
      
        ".html" type="System.Web.Compilation.PageBuildProvider" />
        ".htm" type="System.Web.Compilation.PageBuildProvider" />
      
     



    "false"/>
    
      "ScriptModule"/>
      "ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      "Error404Module" type="ActHJ.ErrorModules.Error404Module"/>
    

 

你可能感兴趣的:([Log]ASP.NET之HttpModule拦截404异常)