0

谁能告诉我,如何在 MVC 5 中使用 Elmah .. 发生错误时,它必须重定向到某个默认页面,例如服务错误,找不到 Http。

public ActionResult About()
{
    ViewBag.Message = "Your app description page.";
    try
    {
        int message = Convert.ToInt32("Hello");
    }
    catch (Exception exp)
    {
        Elmah.ErrorSignal.FromCurrentContext().Raise(exp);
      //  throw;
    }

    return View();
}

我如何重定向到错误页面

4

1 回答 1

0

如前所述,Elmah 主要用于记录错误。重定向到错误页面不是 Elmah 的责任。要查看记录的错误,请点击 Elmah 处理程序

http://yourDomain.com/elmah.axd

现在,如果你想为异常做重定向,你可以做

  1. 在 web.config 中添加 <customErrors mode="On" defaultRedirect="YourErrorPage.htm">
  2. Global.asax 的 Application_Error 中的重定向
  3. 在自定义 HandleErrorAttribute 内重定向(理想情况下,在记录之后)

但是,所有这些都适用于未处理的异常。你的是一个已处理的异常(因为 throw 已被注释)。在这种情况下,您必须直接从 catch 块重定向,例如return Redirect("/YourErrorPage.htm");

于 2015-05-22T18:31:06.550 回答