【问题标题】:ASP.NET MVC 4 Error HandlingASP.NET MVC 4 错误处理
【发布时间】:2012-10-04 22:21:31
【问题描述】:

好的,所以我试图让我的控制器出错时转到Shared 文件夹下的Error.cshtml。我已经在启动时配置了过滤器:

Global.asax

protected void Application_Start()
{
    ...
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    ...
}

FilterConfig.cs

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
}

HomeController.cs

[HandleError(View = "Error")] <---- I have the HandleError attribute
public class HomeController : Controller
{
    IDbConnection _connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);

    [Authorize]
    public ActionResult Index()
    {
        // get the users current events
        try
        {
            ViewBag.UserEvents = _connection.Query<MyEvents>("select ...)", new { });
        }
        catch (Exception ex)
        {
            throw new HttpException(500, ex.Message);
        }

        return View();
    }
    ...
}

因此,当Index 方法因为我没有打开连接而引发异常时,它只会给我默认的 ASP.NET 异常页面。我在这里错过了什么?

谢谢!

【问题讨论】:

  • 使用 HandleError 作为全局过滤器时不需要 HandleErrorAttribute。
  • 我的意思是,当您使用 HandleErrorAttribute 作为全局过滤器时,您不需要 HomeController 上的 [HandleError] 属性。

标签: c# error-handling asp.net-mvc-4


【解决方案1】:

你有没有机会在你的本地机器上运行这个?如果您将 customErrors 设置为 Off 或 RemoteOnly,HandleError 默认情况下不会在本地计算机上显示错误。将其设置为开启。

【讨论】:

  • 谢谢,这正是我需要的,我也按照你的建议拉了属性!
猜你喜欢
  • 1970-01-01
  • 2010-09-16
  • 2018-02-27
  • 2011-06-10
  • 2014-03-26
  • 2013-02-24
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多