Asp.net mvc 3 ASP.NET MVC自定义错误页与神奇独角兽

Asp.net mvc 3 ASP.NET MVC自定义错误页与神奇独角兽,asp.net-mvc-3,http-status-code-404,custom-error-pages,Asp.net Mvc 3,Http Status Code 404,Custom Error Pages,我的问题是关于纯克朗的回答。我试着用他的方法实现我的页面的自定义错误消息,但是有一些问题我不能很好地解释 (a) 当我通过输入无效URL(如localhost:3001/NonexistantPage)引发404错误时,它默认为错误控制器的ServerError()操作,即使它应该转到NotFound()。这是我的错误控制器: 和我的web.config设置: <system.web> <customErrors mode="On" redirectMode="Re

我的问题是关于纯克朗的回答。我试着用他的方法实现我的页面的自定义错误消息,但是有一些问题我不能很好地解释

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


和我的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>
...

...
...
错误视图位于/views/Error/as NotFound.cshtml和ServerError.cshtml中

(b) 有趣的是,当发生服务器错误时,它实际上会显示我定义的服务器错误视图,但是它也会输出一条默认错误消息,并表示找不到错误页面。


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

谢谢

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

视图本身只包含一些文本,没有标记或任何内容


正如我所说的,它实际上在某种程度上起作用,只是不是我想要的工作方式。web.config中的重定向似乎有问题,但我一直无法解决。

我让它工作起来了。一开始我对这个问题的理解似乎有点错误

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

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Views/Error/ServerError.cshtml">
  <error statusCode="404" redirect="~/Views/Error/NotFound.cshtml" />
</customErrors>

。。。 及



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

当您有更复杂的管线和多个管段时,该设置还有一个问题

http://localhost:2902/dsad/dadasdmasda/ddadad/dadads/ddadad/dadadadad/
我得到服务器错误->

Sorry, an error occurred while processing your request.


Exception: An error occured while trying to Render the custom error view which you provided, for this HttpStatusCode. ViewPath: ~/Views/Error/NotFound.cshtml; Message: The RouteData must contain an item named 'controller' with a non-empty string value.
Source: 
我的解决方案是在默认路由之后的末尾添加额外的路由

        routes.MapRoute(
            "Default Catch all 404",
            "{controller}/{action}/{*catchall}",
            new { controller = "Error", action = "NotFound" }
        );

希望它能帮助别人:-)

你也能发表你的观点吗?另外,不要忘记删除这个
GlobalFilters.Filters.Add(newhandleerrorattribute())你能直接导航到404页面吗?是的,我能。这里的问题似乎源于web.config中的设置,尽管我在那里找不到错误。你是对的。我也会把它融入我的路线中。谢谢
http://localhost:2902/dsad/dadasdmasda/ddadad/dadads/ddadad/dadadadad/
Sorry, an error occurred while processing your request.


Exception: An error occured while trying to Render the custom error view which you provided, for this HttpStatusCode. ViewPath: ~/Views/Error/NotFound.cshtml; Message: The RouteData must contain an item named 'controller' with a non-empty string value.
Source: 
        routes.MapRoute(
            "Default Catch all 404",
            "{controller}/{action}/{*catchall}",
            new { controller = "Error", action = "NotFound" }
        );