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

时间:2010-11-19 04:59:21

标签: c# .net asp.net visual-studio

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

<customErrors mode="On" defaultRedirect="~/default.html">
     <error statusCode="404" redirect="~/PageNotFound.html" />
</customErrors>

如果出现404错误,我的网站会重定向到default.html页面,但会将“aspxerrorpath”作为查询字符串参数传递给自定义错误页面,如下所示:

http://www.example.com/default.html?aspxerrorpath=/somepathcausederror/badpage.aspx

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

http://www.example.com/default.html

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

11 个答案:

答案 0 :(得分:69)

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

例如:

<customErrors mode="On" defaultRedirect="errorpage.aspx?error=1" >

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

答案 1 :(得分: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!
    }
}

答案 2 :(得分:6)

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

<customErrors mode="On" defaultRedirect="~/default.html?404"> 
               <error statusCode="404" redirect="~/PageNotFound.html?404" /> 
</customErrors>

答案 3 :(得分:3)

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

答案 4 :(得分:2)

我认为您将在Global.asax中实现/使用Application_Error事件,并在那里进行处理/重定向。

如果您在该处理程序中调用Server.ClearError,我认为它根本不会使用customErrors配置。

答案 5 :(得分:1)

我使用像

这样的javascript
if (location.search != "") { window.location.href = "/404.html"; } 

答案 6 :(得分:0)

如果删除aspxerrorpath = /并且在错误处理期间使用响应重定向,则会出现异常,因此会出现重定向循环。

答案 7 :(得分:0)

为了防止aspxerrorpath问题继续使用ASP.NET CustomErrors支持,我从现在开始实施的最佳解决方案(更多替代方法。。)是重定向到实现错误处理的操作

这是我的解决方案在ASP.NET MVC Web应用程序上下文中的某些步骤:

  

首先在web.config中启用自定义错误模块

<customErrors mode="On" defaultRedirect="~/error/500">
  <error statusCode="404" redirect="~/error/404"/>
</customErrors>
  

然后定义路由规则:

routes.MapRoute(
   name: "Error",
   url: "error/{errorType}/{aspxerrorpath}",
   defaults: new { controller = "Home", action = "Error", errorType = 500, aspxerrorpath = UrlParameter.Optional },
);
  

最后执行以下操作(和相关视图。):

public ActionResult Error(int errorType, string aspxerrorpath)
{
   if (!string.IsNullOrEmpty(aspxerrorpath)) {
      return RedirectToRoute("Error", errorType);
   }

   switch (errorType) {
      case 404:
           return View("~/Views/Shared/Errors/404.cshtml");

       case 500:
        default:
           return View("~/Views/Shared/Errors/500.cshtml");
    }
}

答案 8 :(得分:0)

在“自定义错误”中添加redirectMode="ResponseRewrite"

<customErrors mode="On" defaultRedirect="~/NotFound">
     <error statusCode="404" redirect="~/NotFound" redirectMode="ResponseRewrite"/>
</customErrors>

此解决方案对我有用。

答案 9 :(得分:0)

就我而言,我更喜欢不使用Web.config。然后,我在Global.asax文件中创建了上面的代码:

arn:aws:fsx:region:accountnumber:file-system/fs-********```

那是我的ErrorController.cs

protected void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();

        //Not Found (When user digit unexisting url)
        if(ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
        {
            HttpContextWrapper contextWrapper = new HttpContextWrapper(this.Context);

            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", "Error");
            routeData.Values.Add("action", "NotFound");

            IController controller = new ErrorController();
            RequestContext requestContext = new RequestContext(contextWrapper, routeData);
            controller.Execute(requestContext);
            Response.End();
        }
        else //Unhandled Errors from aplication
        {
            ErrorLogService.LogError(ex);
            HttpContextWrapper contextWrapper = new HttpContextWrapper(this.Context);

            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", "Error");
            routeData.Values.Add("action", "Index");

            IController controller = new ErrorController();
            RequestContext requestContext = new RequestContext(contextWrapper, routeData);
            controller.Execute(requestContext);
            Response.End();
        }
    }

那是我的ErrorLogService.cs

public class ErrorController : Controller
{
    // GET: Error
    public ViewResult Index()
    {
        Response.StatusCode = 500;
        Exception ex = Server.GetLastError();
        return View("~/Views/Shared/SAAS/Error.cshtml", ex);
    }

    public ViewResult NotFound()
    {
        Response.StatusCode = 404;
        return View("~/Views/Shared/SAAS/NotFound.cshtml");
    }
}

答案 10 :(得分:0)

如果要解决或处理错误请求,可以将其插入Handler try catch语句。 像这样:

try {
  //  Block of code that generate error
}
catch(Exception e) {
  //  Block of code to handle errors ||| HERE you can put error in your response and handle it without get xhr redirect error.
}