【问题标题】:MVC 5 Error page not showingMVC 5 错误页面未显示
【发布时间】:2015-05-03 15:51:52
【问题描述】:

我正在编写一个 MVC 5 互联网应用程序并且遇到了错误问题。

当输入一些无效的View 数据时,我收到与显示的错误页面相关的错误。

我正在输入以下内容:

<html>test</html>

在我的Global.asax 文件中,我有以下内容:

protected void Application_Error(object sender, EventArgs e)

这是Application_Error函数中的错误:

Message = "The controller for path '/Error/' was not found or does not implement IController."

在我的Shared 视图文件夹中,我有一个名为Error.cshtml 的错误页面。

这是Error.cshtml的代码:

@model System.Web.Mvc.HandleErrorInfo

@{
    if (TempData["UITitle"] != null)
    {
        ViewBag.Title = @TempData["UITitle"];
    }
    else
    {
        ViewBag.Title = "Error";    
    }
}

<h2 class="text-danger">
    @if (TempData["UIHeading"] != null)
    {
        @TempData["UIHeading"];
    }
    else
    {
        <p>Error</p>
    }
</h2>

<p>
    @if (TempData["UIMessage"] != null)
    {
        @TempData["UIMessage"];
    }
    else
    {
        if (Model != null)
        {
            @Model.Exception.Message
        }
        else
        {
            <p>Error</p>
        }
    }
</p>

为什么会出现错误:

Message = "The controller for path '/Error/' was not found or does not implement IController."

提前致谢。

编辑

我在controller 中添加了以下ActionResult:

public async Task<ActionResult> TestError()
{
    return View("Error");
}

错误页面显示正确。

另外,我安装了ELMAH,并通过电子邮件记录异常。

EDIT2

我认为问题出在我的 web.config 中。

我在 web.config 中有以下内容:

<customErrors mode="On" defaultRedirect="~/Error/">
</customErrors>

每当发生错误时,我都希望在 Shared 文件夹中显示 Error.cshtml。我猜Error.cshtml 找不到,因为它在Shared 文件夹中。

我怎样才能做到这一点?

【问题讨论】:

  • 在控制器中创建一个动作并调用从该动作方法返回这个视图。你能发布你是如何调用这个错误页面的吗?
  • 我已经按照你的要求做了,正在显示错误页面。
  • 所以它工作正常?
  • 你能看看我的编辑吗?

标签: c# exception error-handling asp.net-mvc-5 asp.net-mvc-views


【解决方案1】:

您的错误消息表明您需要一个对应于 URL => '/Error' 的操作方法。

然后此操作应返回View("Error.cshtml");(带有正确的路径)。

如果您的重定向在 web.config 文件中,请尝试以下操作:

<customErrors mode="On" defaultRedirect="~/Views/Shared/Error.cshtml">
</customErrors>

【讨论】:

  • 我收到以下错误:{"Path '/Views/Shared/Error.cshtml' was not found."}
  • @user3736648 是 Views/Shared 文件夹中的 Error.cshtml 文件吗?
  • 是的,我猜它在共享文件夹中时受到保护。
  • 您可以尝试遵循提供的链接prideparrot.com/blog/archive/2012/5/… 中的错误处理过程之一即使您的错误页面加载,您也不会在其中包含模型,因为模型是 MVC 对象并且你已经脱离了 MVC 上下文。文章对此有详尽的解释。
【解决方案2】:

在您的 Global.asax 中添加以下内容

P.S: 根据您的需要更改控制器的名称和操作 根据您的需要声明和其他模组

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        Dim exception As Exception = Server.GetLastError()
        If exception Is Nothing Then
            Exit Sub
        End If
        Dim httpException As HttpException = TryCast(exception, HttpException)
        If httpException IsNot Nothing Then

            Dim routeData As New RouteData()
            routeData.Values.Add("controller", "Error")
            Select Case httpException.GetHttpCode()
                Case 403, 404
                    ' page not found
                    routeData.Values.Add("action", "errorpage404")
                Case Else
                    routeData.Values.Add("action", "errorpage40x)
            End Select
            If httpException.GetHttpCode() = 500 Then
                'TODO
                'Logging
            End If

            'routeData.Values.Add("error", exception)
            ' clear error on server
            Response.Clear()
            Server.ClearError()
            Dim errorController As IController = New ErrorController()
            errorController.Execute(New RequestContext(New HttpContextWrapper(Context), routeData))
        End If
    End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多