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