我想为500、404和403显示一个自定义错误页面。以下是我所做的:

Enabled custom errors in the web.config as follows: <customErrors mode="On" defaultRedirect="~/Views/Shared/Error.cshtml"> <error statusCode="403" redirect="~/Views/Shared/UnauthorizedAccess.cshtml" /> <error statusCode="404" redirect="~/Views/Shared/FileNotFound.cshtml" /> </customErrors> Registered HandleErrorAttribute as a global action filter in the FilterConfig class as follows: public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new CustomHandleErrorAttribute()); filters.Add(new AuthorizeAttribute()); } Created a custom error page for each of the above messages. The default one for 500 was already available out of the box. Declared in each custom error page view that the model for the page is System.Web.Mvc.HandleErrorInfo

对于500,它显示自定义错误页面。对其他人来说,则不然。

我是不是遗漏了什么?

当我阅读HandleErrorAttribute类的OnException方法中的代码时,它看起来并不是显示自定义错误的全部,而且它只处理500个。

我必须做什么来处理其他错误?


当前回答

我做过pablo解决方案,我总是有错误(MVC4)

未找到视图'Error'或其主视图或没有视图引擎支持搜索位置。

要去掉这个,去掉线条

 filters.Add(new HandleErrorAttribute());

在FilterConfig.cs

其他回答

我目前的设置(在MVC3上,但我认为它仍然适用)依赖于有一个ErrorController,所以我使用:

<system.web>
    <customErrors mode="On" defaultRedirect="~/Error">
      <error redirect="~/Error/NotFound" statusCode="404" />
    </customErrors>
</system.web>

控制器包含以下内容:

public class ErrorController : Controller
{
    public ViewResult Index()
    {
        return View("Error");
    }
    public ViewResult NotFound()
    {
        Response.StatusCode = 404;  //you may want to set this to 200
        return View("NotFound");
    }
}

视图就是你实现它们的方式。不过,我倾向于添加一些逻辑,以便在应用程序处于调试模式时显示堆栈跟踪和错误信息。所以错误。CSHTML看起来像这样:

@model System.Web.Mvc.HandleErrorInfo
@{
    Layout = "_Layout.cshtml";
    ViewBag.Title = "Error";
}
<div class="list-header clearfix">
    <span>Error</span>
</div>
<div class="list-sfs-holder">
    <div class="alert alert-error">
        An unexpected error has occurred. Please contact the system administrator.
    </div>
    @if (Model != null && HttpContext.Current.IsDebuggingEnabled)
    {
        <div>
            <p>
                <b>Exception:</b> @Model.Exception.Message<br />
                <b>Controller:</b> @Model.ControllerName<br />
                <b>Action:</b> @Model.ActionName
            </p>
            <div style="overflow:scroll">
                <pre>
                    @Model.Exception.StackTrace
                </pre>
            </div>
        </div>
    }
</div>

我建议使用Global.asax.cs文件。

 protected void Application_Error(Object sender, EventArgs e)
{
    var exception = Server.GetLastError();
    if (exception is HttpUnhandledException)
    {
        Server.Transfer("~/Error.aspx");
    }
    if (exception != null)
    {
        Server.Transfer("~/Error.aspx");
    }
    try
    {
        // This is to stop a problem where we were seeing "gibberish" in the
        // chrome and firefox browsers
        HttpApplication app = sender as HttpApplication;
        app.Response.Filter = null;
    }
    catch
    {
    }
}

我已经设置好了一切,但仍然无法在登台服务器上看到状态代码500的正确错误页面,尽管在本地开发服务器上一切正常。

我发现Rick Strahl的这篇博文对我很有帮助。

我需要添加响应。TrySkipIisCustomErrors = true;到我的自定义错误处理代码。

这里似乎有许多步骤混杂在一起。我会把我从头开始做的事情说出来。

Create the ErrorPage controller public class ErrorPageController : Controller { public ActionResult Index() { return View(); } public ActionResult Oops(int id) { Response.StatusCode = id; return View(); } } Add views for these two actions (right click -> Add View). These should appear in a folder called ErrorPage. Inside App_Start open up FilterConfig.cs and comment out the error handling filter. public static void RegisterGlobalFilters(GlobalFilterCollection filters) { // Remove this filter because we want to handle errors ourselves via the ErrorPage controller //filters.Add(new HandleErrorAttribute()); } Inside web.config add the following <customerErrors> entries, under System.Web <customErrors mode="On" defaultRedirect="~/ErrorPage/Oops"> <error redirect="~/ErrorPage/Oops/404" statusCode="404" /> <error redirect="~/ErrorPage/Oops/500" statusCode="500" /> </customErrors> Test (of course). Throw an unhandled exception in your code and see it go to the page with id 500, and then use a URL to a page that does not exist to see 404.

我做过pablo解决方案,我总是有错误(MVC4)

未找到视图'Error'或其主视图或没有视图引擎支持搜索位置。

要去掉这个,去掉线条

 filters.Add(new HandleErrorAttribute());

在FilterConfig.cs