使用CustomErrors删除aspxerrorpath param使用ResponseRewrite

时间:2014-03-25 09:53:44

标签: asp.net-mvc-4 custom-error-pages

我正在使用asp.net MVC4 + visual studio 2012.一切都很好,但只有自定义错误总是在URL上有 aspxerrorpath 参数。

我已经在 web.config 上配置了自定义错误:

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

我还将错误操作更改为:

public ActionResult Error()
{
    Response.Status = "404 Not Found";
    Response.StatusCode = 404;
    return View();
}

现在有404会发生。我的URL上总是有aspxerrorpath param。 我尝试将redirectMode="ResponseRewrite" 添加到customError节点但是如果添加此项,则错误将显示运行时异常.....

那么有没有最好的方法来删除aspxerrorpath param?感谢。

7 个答案:

答案 0 :(得分:6)

另一个简单的解决方案是在web.config文件中启用customErrors以获取404错误:

<customErrors mode="On">
  <error statusCode="404" redirect="~/home/notfound" />
</customErrors>

并在家庭控制器中:

public ActionResult NotFound(string aspxerrorpath)
{
    if (!string.IsNullOrWhiteSpace(aspxerrorpath))
        return RedirectToAction("NotFound");

    return View();
}

答案 1 :(得分:2)

我写了

<customErrors mode="On" defaultRedirect="~/" redirectMode="ResponseRedirect" />
<system.web>...</system.web>中的

,而不是在我的默认操作(在默认控制器中),在我写的第一行:

if (Request["aspxerrorpath"] != null) return RedirectToAction(this.ControllerContext.RouteData.Values["action"].ToString());

这是肮脏的决心,但有效。

答案 2 :(得分:0)

我遇到同样的问题,我的IIS服务器设置了aspx错误路径。所以检查你的IIS设置:

your project > Authentication > Forms Authentic

在表单身份验证中右键单击“编辑”并检查您的URL。

我遇到问题,即网址路径删除了扩展程序.aspx并且工作正常。

答案 3 :(得分:0)

在mvc操作中使用重定向。

例如在配置中,您可以拥有&#34;〜/错误&#34;,在控制器中有重定向到错误操作的索引操作(默认操作)(我将其称为PageNotFound) - 重定向时将丢失查询字符串。

答案 4 :(得分:0)

最后我关闭了customErrors

<customErrors  mode="Off"></customErrors>

然后我使用 httpErrors 来替换它。

<httpErrors  errorMode="Custom" existingResponse="Replace">
      <remove statusCode="400"/>
      <error statusCode="400" responseMode="ExecuteURL" path="/Error/BadRequest"/>
      <remove statusCode="403"/>
      <error statusCode="403" responseMode="ExecuteURL" path="/Error/AccessDenied" />
          <remove statusCode="404"/>
      <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
      <remove statusCode="500"/>
      <error statusCode="500" responseMode="ExecuteURL" path="/Error/Error" />
</httpErrors>

现在一切都好。

更新1

但有时会显示异常详情,现在我使用:

<customErrors mode="RemoteOnly" defaultRedirect="~/Main/Error" redirectMode="ResponseRewrite">
      <error redirect="~/Main/NotFound" statusCode="404" />
      <error redirect="~/Main/Error" statusCode="500" />
      <error redirect="~/Main/AccessDenied" statusCode="403" />
      <error redirect="~/Main/BadRequest" statusCode="400" />
    </customErrors> 


 <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="400" />
      <error statusCode="400" responseMode="ExecuteURL" path="/Main/BadRequest" />
      <remove statusCode="403" />
      <error statusCode="403" responseMode="ExecuteURL" path="/Main/AccessDenied" />
      <remove statusCode="404" />
      <error statusCode="404" responseMode="ExecuteURL" path="/Main/NotFound" />
      <remove statusCode="500" />
      <error statusCode="500" responseMode="ExecuteURL" path="/Main/Error" />
    </httpErrors>

我认为现在还没有找到最佳解决方案。也不明白为什么微软这样设计。也许希望每个人都知道基于 .NET

的网站

答案 5 :(得分:0)

<customErrors mode="On" defaultRedirect="/404.html"  redirectMode="ResponseRewrite">
    <error redirect="/403.html" statusCode="403"  />
    <error redirect="/404.html" statusCode="404" />
    <error redirect="/500.html" statusCode="500" />
  </customErrors>

答案 6 :(得分:0)

最简单的绝对解决方案是在web.config的defaultRedirect中使用空白查询字符串进行覆盖(注意问号):

<system.web>
    <customErrors defaultRedirect="~/error?" mode="On" />
</system.web>