如何优先IIS拦截404错误?就是找不到页面错误?或者有其他办法得到错误参数?

时间:2022-09-03 20:12:48
如题,假如请求一个不存在的文件地址,希望由我的程序优先IIS得到404错误。我在WEBCONFIG中加入了转向处理页面,但是这样作的话只能得到不存在的文件名。
却无法得到原请求的参数。我尝试读取了很多内容,包括session httprequest httpheader等等,就是没有原连接的参数。

请问如何处理这个问题?
有人说要我写个HTTPMODEL之后就从系消失了。我也没找到如何处理。。

谢谢大家指点

比如 IIS上没有 ABC.ASPX页面
当请求
http://xxxx/abc.aspx?id=34&hd=fddd
原本的自定义错误404 只能得到/abc.aspx 却得不到后便的参数。
而且很多例子里提到了 Appliction 里写Error方法,取GetLastError异常。但是实际上我怎么取都是 NULL

10 个解决方案

#1


Request.ServerVariables("HTTP_Referer")     
这样获得的是整个的url

#2


把请求语句放在try...catch块中,当请求的页面不存在时便会发生异常,此时可捕获异常信息,里面应该有详细的错误信息,包括请求的页面。你试试吧。

#3


重写HttpModule的方法

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// TestModule 的摘要说明
/// </summary>
public class TestModule : IHttpModule
{
    public TestModule()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    #region IHttpModule 成员

    public void Dispose()
    {
        return;
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
    }

    void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;

        string url = app.Request.RawUrl; //你要的链接
    }

    #endregion
}

TestModule类放在App_Code文件夹

WebConfig配置
<httpModules>
<add name="TestModule" type="TestModule" />
</httpModules>

#4


引用 1 楼 wanghao3616 的回复:
Request.ServerVariables("HTTP_Referer")    
这样获得的是整个的url


这个方法再 系统出现404以后是根本无效的。404转向是IIS作的 不是页面间跳转的。

#5


这个办法看哎可行 我去试下  不过不知道 自己去每个请求都检查的效率如何啊?

#6


我觉得这个功能好像很难做到

尤其对于一些html或者html+ajax页面

#7


程序出现异常,信息如下: 

Offending URL: http://localhost:1034/NanFangMgr/Login/Login22.aspx?aaa=1&bb=2
Source: System.Web
Message: 文件“/NanFangMgr/Login/Login22.aspx”不存
在。
TargetSite: Void CheckVirtualFileExists(System.Web.VirtualPath)  
----------------------------------------------------------------------
如上述得到的异常提示信息是不是lz需要的,这是在Global.asax中处理的,内容如下:
 protected void Application_Error(object sender, EventArgs e)
        {  #region
            
            // 在出现未处理的错误时运行的代码
             HttpContext ctx = HttpContext.Current;
            Exception exception = ctx.Server.GetLastError().GetBaseException();
            string errorInfo =
            "<br>Offending URL: " + ctx.Request.Url.ToString() +
            "<br>Source: " + exception.Source +
            "<br>Message: " + exception.Message +
            "<br>TargetSite: " + exception.TargetSite;// 
            string vPath = HttpContext.Current.Request.ApplicationPath;
            if (vPath == "/")
            {
                vPath = "";
            }
            string Port = ":" + HttpContext.Current.Request.Url.Port.ToString();
            if (Port == ":80")
            {
                Port = "";
            }
            string Address = "http://" + HttpContext.Current.Request.Url.Host + Port + vPath + "/";
            Application["Erro"] = errorInfo;//errorInfo
            Response.Redirect(Address + "ErrorPage.aspx");           
            #endregion

        }

#8


楼上的   我这样早试过了啊。。。 但是如果 从 IE里直接输入地址 这个东西就 取出的异常信息都是NULL  
HttpContext.Current; 得到的里边也没有 Request.Url啊 

#9


引用 6 楼 yeness 的回复:
我觉得这个功能好像很难做到 

尤其对于一些html或者html+ajax页面

不是难,是不可能.LZ换个思路吧.

#10


在 BeginRequest 事件中可以拦截任何请求文件包括非 .aspx  

// Global.asax.cs
 void Application_BeginRequest(object sender, EventArgs e)
 {
         // Request.RawUrl , 注意这里 Session 还无法使用,因此需要考虑其他方式存储,可能是文件、数据库
 }

#1


Request.ServerVariables("HTTP_Referer")     
这样获得的是整个的url

#2


把请求语句放在try...catch块中,当请求的页面不存在时便会发生异常,此时可捕获异常信息,里面应该有详细的错误信息,包括请求的页面。你试试吧。

#3


重写HttpModule的方法

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// TestModule 的摘要说明
/// </summary>
public class TestModule : IHttpModule
{
    public TestModule()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    #region IHttpModule 成员

    public void Dispose()
    {
        return;
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
    }

    void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;

        string url = app.Request.RawUrl; //你要的链接
    }

    #endregion
}

TestModule类放在App_Code文件夹

WebConfig配置
<httpModules>
<add name="TestModule" type="TestModule" />
</httpModules>

#4


引用 1 楼 wanghao3616 的回复:
Request.ServerVariables("HTTP_Referer")    
这样获得的是整个的url


这个方法再 系统出现404以后是根本无效的。404转向是IIS作的 不是页面间跳转的。

#5


这个办法看哎可行 我去试下  不过不知道 自己去每个请求都检查的效率如何啊?

#6


我觉得这个功能好像很难做到

尤其对于一些html或者html+ajax页面

#7


程序出现异常,信息如下: 

Offending URL: http://localhost:1034/NanFangMgr/Login/Login22.aspx?aaa=1&bb=2
Source: System.Web
Message: 文件“/NanFangMgr/Login/Login22.aspx”不存
在。
TargetSite: Void CheckVirtualFileExists(System.Web.VirtualPath)  
----------------------------------------------------------------------
如上述得到的异常提示信息是不是lz需要的,这是在Global.asax中处理的,内容如下:
 protected void Application_Error(object sender, EventArgs e)
        {  #region
            
            // 在出现未处理的错误时运行的代码
             HttpContext ctx = HttpContext.Current;
            Exception exception = ctx.Server.GetLastError().GetBaseException();
            string errorInfo =
            "<br>Offending URL: " + ctx.Request.Url.ToString() +
            "<br>Source: " + exception.Source +
            "<br>Message: " + exception.Message +
            "<br>TargetSite: " + exception.TargetSite;// 
            string vPath = HttpContext.Current.Request.ApplicationPath;
            if (vPath == "/")
            {
                vPath = "";
            }
            string Port = ":" + HttpContext.Current.Request.Url.Port.ToString();
            if (Port == ":80")
            {
                Port = "";
            }
            string Address = "http://" + HttpContext.Current.Request.Url.Host + Port + vPath + "/";
            Application["Erro"] = errorInfo;//errorInfo
            Response.Redirect(Address + "ErrorPage.aspx");           
            #endregion

        }

#8


楼上的   我这样早试过了啊。。。 但是如果 从 IE里直接输入地址 这个东西就 取出的异常信息都是NULL  
HttpContext.Current; 得到的里边也没有 Request.Url啊 

#9


引用 6 楼 yeness 的回复:
我觉得这个功能好像很难做到 

尤其对于一些html或者html+ajax页面

不是难,是不可能.LZ换个思路吧.

#10


在 BeginRequest 事件中可以拦截任何请求文件包括非 .aspx  

// Global.asax.cs
 void Application_BeginRequest(object sender, EventArgs e)
 {
         // Request.RawUrl , 注意这里 Session 还无法使用,因此需要考虑其他方式存储,可能是文件、数据库
 }