自定义错误页面未被调用

R.C*_*R.C 7 asp.net asp.net-mvc asp.net-mvc-4

我需要在MVC 4中实现我自己的自定义错误页面.基本上,当用户尝试查看Details任何不存在productID的产品时,我想要这个自定义错误页面.

我创建了自己的自定义错误页面 NotFound.aspx

此页面的内容包括:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Simple.Master" 
Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %> 
<asp:Content ID="errorTitle" ContentPlaceHolderID="TitleContent" runat="server"> 
Error 
</asp:Content> 
<asp:Content ID="errorContent" ContentPlaceHolderID="MainContent" runat="server"> 
<h2> 
Sorry, you requested a product that doesn't exist. Make sure you
requested a valid ProductID 
</h2> 
</asp:Content>
Run Code Online (Sandbox Code Playgroud)

然后将HandleError过滤器应用到我的ActionMethod:Detailsas:

[HandleError(View="NotFound")] 
public ActionResult Details(int id) {...
Run Code Online (Sandbox Code Playgroud)

问题是,始终默认视图: Views/Shared/Error.aspx正在调用,而不是新的自定义错误页面.有什么想法吗 ?

R.C*_*R.C 1

解决方案是我的ProductController类还需要将order属性设置为:

\n\n
[HandleError(Order=2)] \npublic class ProductController : Controller { ... }\n
Run Code Online (Sandbox Code Playgroud)\n\n

这意味着:阶数值为 2 将确保仅当不存在\xe2\x80\x99t 具有更高阶数的 \nHandleError 筛选器时才会应用控制器范围的筛选器。

\n\n

这非常有效。我的 web.config 设置是:\n <customErrors mode="On" />。

\n\n

就是这个。根本defaultRedirect不需要。

\n\n

注意: \n我HandleError最初的过滤器没有 order属性。

\n\n
[HandleError] \npublic class ProductController : Controller { ... }\n
Run Code Online (Sandbox Code Playgroud)\n\n

当您应用不带参数的 HandleError 过滤器时,您指定过滤器涵盖的方法\n抛出的任何异常都将导致视图Views/Shared/Error.aspx被使用。

\n