ASP.NET MVC自定义错误页面与魔法独角兽

FLC*_*ver 5 custom-error-pages http-status-code-404 asp.net-mvc-3

我的问题是关于Pure.Kromes对这篇文章的回答.我尝试使用他的方法实现我的页面的自定义错误消息,但有些问题我无法解释.

a)当我通过输入无效的URL(例如localhost:3001/NonexistantPage)引发404错误时,它默认为我的错误控制器的ServerError()Action,即使它应该转到NotFound().这是我的ErrorController:

    public class ErrorController : Controller
    {
        public ActionResult NotFound()
        {
            Response.TrySkipIisCustomErrors = true;
            Response.StatusCode = (int)HttpStatusCode.NotFound;
            var viewModel = new ErrorViewModel()
            {
                ServerException = Server.GetLastError(),
                HTTPStatusCode = Response.StatusCode
            };
            return View(viewModel);
        }

        public ActionResult ServerError()
        {
            Response.TrySkipIisCustomErrors = true;
            Response.StatusCode = (int)HttpStatusCode.InternalServerError;

            var viewModel = new ErrorViewModel()
            {
                ServerException = Server.GetLastError(),
                HTTPStatusCode = Response.StatusCode
            };
            return View(viewModel);
        }
    }

我在Global.asax.cs中的错误路由:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

        routes.MapRoute(
            name: "Error - 404",
            url: "NotFound",
            defaults: new { controller = "Error", action = "NotFound" }
            );

        routes.MapRoute(
            name: "Error - 500",
            url: "ServerError",
            defaults: new { controller = "Error", action = "ServerError" }
            );
Run Code Online (Sandbox Code Playgroud)


我的web.config设置:

<system.web>
    <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/ServerError">
    <error statusCode="404" redirect="/NotFound" />
</customErrors>
...
</system.web>

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" path="/NotFound" responseMode="ExecuteURL" />
      <remove statusCode="500" subStatusCode="-1" />
      <error statusCode="500" path="/ServerError" responseMode="ExecuteURL" />
</httpErrors>
...
Run Code Online (Sandbox Code Playgroud)

Error视图位于/ Views/Error /中,作为NotFound.cshtml和ServerError.cshtml.

b)一个有趣的事情是,当发生服务器错误时,它实际上显示了我定义的服务器错误视图,但是它也输出了一条默认错误消息,说明找不到错误页面.

这是它的样子:


你对我如何解决这两个问题有什么建议吗?我真的很喜欢Pure.Kromes实现这些错误消息的方法,但如果有更好的方法来实现这一点,请不要犹豫告诉我.

谢谢!

**编辑:**我可以通过访问/ Error/NotFound或Error/ServerError直接导航到我的视图通过ErrorController.

视图本身只包含一些文本,没有标记或任何东西.

正如我所说,它实际上在某种程度上起作用,而不是我想要它的工作方式.web.config中的重定向似乎存在问题,但我无法弄明白.

FLC*_*ver 0

我让它工作了。看来我对这个问题的理解一开始就有些错误。

在 web.config 中,我更改了以下内容:

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Views/Error/ServerError.cshtml">
  <error statusCode="404" redirect="~/Views/Error/NotFound.cshtml" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)

... 和 ...

<httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" path="/NotFound" responseMode="ExecuteURL" />
      <remove statusCode="500" subStatusCode="-1" />
      <error statusCode="500" path="/ServerError" responseMode="ExecuteURL" />
    </httpErrors>
Run Code Online (Sandbox Code Playgroud)

这直接重定向到视图。我的理解是我必须重定向到错误控制器,而错误控制器又会重定向到视图,但显然情况并非如此。我要感谢您的评论,因为它们让我再次分析问题,而当时我已经准备放弃自定义错误内容并只是偷懒并显示 YSOD。:)