如何防止"aspxerrorpath"作为查询字符串传递给ASP.NET自定义错误页面

DSh*_*per 38 .net c# asp.net visual-studio

在我的ASP.NET Web应用程序中,我在web.config文件中定义了自定义错误页面,如下所示:

<customErrors mode="On" defaultRedirect="~/default.html">
     <error statusCode="404" redirect="~/PageNotFound.html" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)

在404错误的情况下,我的站点重定向到default.html页面,但它将"aspxerrorpath"作为查询字符串参数传递给自定义错误页面,如下所示:

http://www.example.com/default.html?aspxerrorpath=/somepathcausederror/badpage.aspx
Run Code Online (Sandbox Code Playgroud)

我不希望这种行为.我希望重定向网址只需阅读:

http://www.example.com/default.html
Run Code Online (Sandbox Code Playgroud)

有没有办法实现这个目标?

Mat*_* J. 70

如果在指定路径时提供自己的查询字符串变量,那么.NET将不会添加"aspxerrorpath".谁知道?

例如:

<customErrors mode="On" defaultRedirect="errorpage.aspx?error=1" >
Run Code Online (Sandbox Code Playgroud)

这样就可以了.我不得不将这个添加到一堆应用程序,因为默认情况下,URLScan for IIS拒绝任何包含"aspxerrorpath"的内容.

  • 值得一提的是你真正需要做的就是添加一个"?" 到最后,以防止添加aspxerrorpath参数.所以只是:`<customErrors mode ="On"defaultRedirect ="〜/ default.html?"> </ customErrors>`会做的伎俩 (23认同)

Gar*_*ams 11

在global.asax中,捕获404错误并重定向到找不到文件的页面.我不需要aspxerrorpath,它对我有用.

void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
    {
        Response.Redirect("~/filenotfound.aspx");
    }
    else
    {
        // your global error handling here!
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 6

您可以将自己的url参数发送到错误页面

<customErrors mode="On" defaultRedirect="~/default.html?404"> 
               <error statusCode="404" redirect="~/PageNotFound.html?404" /> 
</customErrors>
Run Code Online (Sandbox Code Playgroud)


Mik*_*son 2

我的第一个想法是创建一个 HttpHandler 来捕获aspxerrorpath其中的 url,并将其剥离。您也可以对 IIS7 中的重写模块执行相同的操作。