【发布时间】: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