c# - ASP.NET MVC 4 错误处理

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

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

全局.asax

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

过滤配置.cs

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

家庭 Controller .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 异常页面。我在这里错过了什么?

谢谢!

最佳答案

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

关于c# - ASP.NET MVC 4 错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12887232/

相关文章:

c# - SP 在 EF 5 CF 中有多个结果

C# Regex 使用 newLine 在两个字符串之间查找字符串

c# - 当我使用 VS 2010 SP1 Beta 打开一个新项目时,ASP.NET MVC 3 挂起

mysql - max_allowed_pa​​cket=1024M... 它仍然给我错误 "Got a packet bigger than ' max_allowed_pa​​cket' bytes query...”

javascript - jQuery ajax $没有定义,但是仍然可以正确执行吗?

localization - 用于 Web 应用程序的多语言应用程序工具包

c# - .NET 4.0 中的 CallerMemberName 不起作用

c# - 使用数据模板 (WPF) 在列表框中内联编辑 TextBlock

python - 如何在 np.linalg.inv 函数的奇异矩阵上传递引发的错误?

asp.net-mvc - Kendo Grid 未正确排序字母数字列(自然排序)