我想为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个。

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


当前回答

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

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

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

其他回答

我已经设置好了一切,但仍然无法在登台服务器上看到状态代码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.

基于maxspan发布的答案,我在GitHub上整理了一个最小的示例项目,展示了所有的工作部分。

基本上,我们只是在global.asax.cs中添加了一个Application_Error方法来拦截异常,并让我们有机会重定向(或者更准确地说,传输请求)到一个自定义错误页面。

    protected void Application_Error(Object sender, EventArgs e)
    {
        // See http://stackoverflow.com/questions/13905164/how-to-make-custom-error-pages-work-in-asp-net-mvc-4
        // for additional context on use of this technique

        var exception = Server.GetLastError();
        if (exception != null)
        {
            // This would be a good place to log any relevant details about the exception.
            // Since we are going to pass exception information to our error page via querystring,
            // it will only be practical to issue a short message. Further detail would have to be logged somewhere.

            // This will invoke our error page, passing the exception message via querystring parameter
            // Note that we chose to use Server.TransferRequest, which is only supported in IIS 7 and above.
            // As an alternative, Response.Redirect could be used instead.
            // Server.Transfer does not work (see https://support.microsoft.com/en-us/kb/320439 )
            Server.TransferRequest("~/Error?Message=" + exception.Message);
        }

    }

错误的控制器:

/// <summary>
/// This controller exists to provide the error page
/// </summary>
public class ErrorController : Controller
{
    /// <summary>
    /// This action represents the error page
    /// </summary>
    /// <param name="Message">Error message to be displayed (provided via querystring parameter - a design choice)</param>
    /// <returns></returns>
    public ActionResult Index(string Message)
    {
        // We choose to use the ViewBag to communicate the error message to the view
        ViewBag.Message = Message;
        return View();
    }

}

错误页面

<!DOCTYPE html>

<html>
<head>
    <title>Error</title>
</head>
<body>

    <h2>My Error</h2>
    <p>@ViewBag.Message</p>
</body>
</html>

除了禁用/删除过滤器,不涉及其他任何内容。在FilterConfig.cs中添加(new HandleErrorAttribute())

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        //filters.Add(new HandleErrorAttribute()); // <== disable/remove
    }
}

虽然实现起来非常简单,但我发现这种方法的一个缺点是使用querystring将异常信息传递到目标错误页面。

这是我的解决方案。使用[ExportModelStateToTempData] / [ImportModelStateFromTempData]在我看来是不舒服的。

~ Views / Home / Error.cshtml:

@{
    ViewBag.Title = "Error";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Error</h2>
<hr/>

<div style="min-height: 400px;">

    @Html.ValidationMessage("Error")

    <br />
    <br />

    <button onclick="Error_goBack()" class="k-button">Go Back</button>
    <script>
        function Error_goBack() {
            window.history.back()
        }
    </script>

</div>

~ /控制器/ HomeController.sc:

public class HomeController : BaseController
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Error()
    {
        return this.View();
    }

    ...
}

~ /控制器/ BaseController.sc:

public class BaseController : Controller
{
    public BaseController() { }

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.Result is ViewResult)
        {
            if (filterContext.Controller.TempData.ContainsKey("Error"))
            {
                var modelState = filterContext.Controller.TempData["Error"] as ModelState;
                filterContext.Controller.ViewData.ModelState.Merge(new ModelStateDictionary() { new KeyValuePair<string, ModelState>("Error", modelState) });
                filterContext.Controller.TempData.Remove("Error");
            }
        }
        if ((filterContext.Result is RedirectResult) || (filterContext.Result is RedirectToRouteResult))
        {
            if (filterContext.Controller.ViewData.ModelState.ContainsKey("Error"))
            {
                filterContext.Controller.TempData["Error"] = filterContext.Controller.ViewData.ModelState["Error"];
            }
        }

        base.OnActionExecuted(filterContext);
    }
}

~ /控制器/ MyController.sc:

public class MyController : BaseController
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Details(int id)
    {
        if (id != 5)
        {
            ModelState.AddModelError("Error", "Specified row does not exist.");
            return RedirectToAction("Error", "Home");
        }
        else
        {
            return View("Specified row exists.");
        }
    }
}

祝你们项目成功;-)

在网络上。在系统下添加此配置。Webserver标签如下所示,

<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404"/>
  <remove statusCode="500"/>
  <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound"/>
  <error statusCode="500" responseMode="ExecuteURL"path="/Error/ErrorPage"/>
</httpErrors>

并添加控制器为,

public class ErrorController : Controller
{
    //
    // GET: /Error/
    [GET("/Error/NotFound")]
    public ActionResult NotFound()
    {
        Response.StatusCode = 404;

        return View();
    }

    [GET("/Error/ErrorPage")]
    public ActionResult ErrorPage()
    {
        Response.StatusCode = 500;

        return View();
    }
}

加上他们受人尊敬的观点,我想这对所有人都肯定有效。

这个解决方案是我从《Neptune Century》中找到的