MVC 5 错误页面未显示
MVC 5 Error page not showing
我正在编写一个 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
文件夹中。
我怎样才能做到这一点?
您的错误消息表明您需要一个对应于 URL => '/Error' 的操作方法。
此操作应 return View("Error.cshtml");
(使用正确的路径)。
如果您的重定向在 web.config 文件中,请尝试以下操作:
<customErrors mode="On" defaultRedirect="~/Views/Shared/Error.cshtml">
</customErrors>
在您的 Global.asax
中添加以下内容
P.S: Change the name of the contr4oller and action as per your
declarations and other mods as per ur need
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
我正在编写一个 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
文件夹中。
我怎样才能做到这一点?
您的错误消息表明您需要一个对应于 URL => '/Error' 的操作方法。
此操作应 return View("Error.cshtml");
(使用正确的路径)。
如果您的重定向在 web.config 文件中,请尝试以下操作:
<customErrors mode="On" defaultRedirect="~/Views/Shared/Error.cshtml">
</customErrors>
在您的 Global.asax
P.S: Change the name of the contr4oller and action as per your declarations and other mods as per ur need
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