c# - 找不到路径 '/Views/Shared/Error.cshtml'

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

我的基本 Controller 中有这个

protected override void OnException(ExceptionContext filterContext)
{
    if (filterContext.ExceptionHandled)
    {
        return;
    }

    var logger = new Logger();
    logger.LogError(filterContext.Exception, ControllerContext.RouteData.Values["controller"].ToString());

    filterContext.Result = new ViewResult
    {
        ViewName = "~/Views/Shared/Error.cshtml",
        ViewData = new ViewDataDictionary()
        {
            {"exception", filterContext.Exception}
        }
    };
    filterContext.ExceptionHandled = true;
}

我肯定在
Views/Shared/Error.cshtml

enter image description here

但是,无论何时发生未处理的异常,我都会在日志中收到此错误

Path '/Views/Shared/Error.cshtml' was not found.



但是根据this article这应该工作...

这怎么可能?

最佳答案

我认为您不需要全名,因为它是默认页面。你试一试

filterContext.Result = new ViewResult
{
    ViewName = "Error",
    ViewData = new ViewDataDictionary()
    {
        {"exception", filterContext.Exception}
    }
};

希望这对您有用!

关于c# - 找不到路径 '/Views/Shared/Error.cshtml',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50559511/

相关文章:

c# - Azure 表查询在更改大小写时返回空值

c# - 将 DateTime 从 UTC 转换为用户位置时区

asp.net - 如何在 MVC 中使用参数进行搜索?

c# - Silverlight 中的 TemplateBinding 和附加字符串

c# - 如果更改了其中一个 Controller 方法,我是否应该将每个 Controller 方法更改为异步?

c# - 为什么我的 Razor 'RenderSection' 没有被孙 View 继承?

php - PHP 中的应用程序错误与用户错误

excel - 如何避免vba中的双重打开

jsf - 使用JSF/Facelets处理带有后备bean问题的页面时出错

c# - ClickOnce 发布错误。无法复制 dll.deploy 文件