如何在ASP.NET MVC中使用[HandleError]属性?

时间:2009-10-21 04:06:41

标签: asp.net-mvc error-handling

我的ASP.NET MVC网站中有一些自定义错误处理 - >似乎工作正常。

我有一个错误控制器,如下所示: -

/Controller/ErrorController.cs

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Unknown()
{
    Response.StatusCode = (int)HttpStatusCode.InternalServerError;
    return View("Error");
}

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult NotFound()
{
    Response.StatusCode = (int)HttpStatusCode.NotFound;
    return View("Error");
}

(我还删除了其他一些文字,比如要显示的信息等。)

视图位于..

/Views/Error/Error.aspx

现在,我不确定在发生错误时如何“调用”Unknown方法(上图)。我以为我可以使用[HandleError]属性,但这只允许我指定一个视图(我不认为我正在这样做,导致错误网址真的很奇怪)。

1 个答案:

答案 0 :(得分:0)

[HandleError]属性允许您为控制器类中的未处理异常指定错误页面,而不是ASP.NET提供的默认自定义错误处理。 我宁愿使用传统的ASP.NET自定义错误处理,除非在某些控制器中你想要默认的自定义错误行为。

 <customErrors mode="On" defaultRedirect="~/Error/Unknown">
      <error statusCode="404" redirect="~/Error/PageNotFound"></error>
      <error statusCode="500" redirect="~/Error/Server"></error>
      <error statusCode="503" redirect="~/Error/Server"></error>
    </customErrors>
相关问题