具有自定义错误页面的MVC InvalidOperationException [英] MVC InvalidOperationException with custom error pages
问题描述
我使用以下方法自定义错误页面设置
I have custom error pages setup using
<customErrors mode="On" defaultRedirect="~/Home/Error">
<error statusCode="404" redirect="~/Home/PageNotFound" />
</customErrors>
我创建了一个引发异常的页面,然后将我重定向到正确的错误页面.
I created a page that throws and exception and I get redirected to the correct error pages.
但是我在生产Web服务器上的elmah中注意到了这些错误:
However I am noticing these errors in elmah on the production webserver:
System.InvalidOperationException视图错误"或其主视图为 找不到或视图引擎不支持搜索到的位置.这 搜索了以下位置: 〜/区域/足球/视图/草稿/Error.aspx 〜/区域/足球/视图/草稿/Error.ascx 〜/区域/足球/视图/共享/Error.aspx 〜/区域/足球/视图/共享/Error.ascx〜/视图/草稿/Error.aspx 〜/Views/草稿/Error.ascx〜/Views/Shared/Error.aspx 〜/Views/Shared/Error.ascx〜/Areas/Football/Views/Draft/Error.cshtml 〜/区域/足球/视图/草稿/Error.vbhtml 〜/区域/足球/视图/共享/Error.cshtml 〜/Areas/Football/Views/Shared/Error.vbhtml〜/Views/Draft/Error.cshtml 〜/Views/Draft/Error.vbhtml〜/Views/Shared/Error.cshtml 〜/Views/Shared/Error.vbhtml
System.InvalidOperationException The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Areas/Football/Views/Draft/Error.aspx ~/Areas/Football/Views/Draft/Error.ascx ~/Areas/Football/Views/Shared/Error.aspx ~/Areas/Football/Views/Shared/Error.ascx ~/Views/Draft/Error.aspx ~/Views/Draft/Error.ascx ~/Views/Shared/Error.aspx ~/Views/Shared/Error.ascx ~/Areas/Football/Views/Draft/Error.cshtml ~/Areas/Football/Views/Draft/Error.vbhtml ~/Areas/Football/Views/Shared/Error.cshtml ~/Areas/Football/Views/Shared/Error.vbhtml ~/Views/Draft/Error.cshtml ~/Views/Draft/Error.vbhtml ~/Views/Shared/Error.cshtml ~/Views/Shared/Error.vbhtml
为什么要在其他地方查找错误页面?我删除了〜/Views/Shared/Error.cshtml,并在〜/Home/Error中添加了自定义错误页面,因为我在配置文件中指定了新的默认值.
Why is it looking for the error page elsewhere? I deleted ~/Views/Shared/Error.cshtml and added my custom error page at ~/Home/Error since i specified a new default in my config file.
有什么想法吗?
谢谢.
推荐答案
MVC projects by default adds the HandleErrorAttribute in the Global.asax.cs
file
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
当引发未处理的异常时,将执行此过滤器.它将视图设置为Error
.因此,MVC运行时尝试呈现该视图.但是就您而言,没有这样的观点.因此,它再次引发由ASP.NET运行时处理的另一个异常,并显示您在Web.Config
文件中配置的错误页面.
This filter is executed when an unhandled exception is thrown. It sets the view as Error
. Hence MVC runtime tries to render that view. But in your case, there is no such view. So it again throws another exception which is handled by ASP.NET runtime and shows your error page that you have configured in Web.Config
file.
您可以创建自己的异常过滤器并进行注册.
You can create your own exception filter and register it.
这篇关于具有自定义错误页面的MVC InvalidOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!