2012-10-31 56 views
5

我的问题是关于Pure.Kromes答案this post。我尝试使用他的方法实现我的页面的自定义错误消息,但有一些问题我无法解释。ASP.NET MVC自定义错误页面与魔法独角兽

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

 
    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); 
     } 
    } 

我的错误路线:

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" } 
      ); 


我的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) 一个有趣的事情是,当发生服务器错误时,它实际上会显示我定义的服务器错误视图,但它也会输出一条默认错误消息,并说错误页面找不到。

Here's how it looks like:


你有什么建议,我怎么能解决这两个问题?我非常喜欢Pure.Kromes的方法来实现这些错误信息,但是如果有更好的方法来实现这一点,不要犹豫,告诉我。

谢谢!

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

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

正如我所说的,它实际上以某种方式工作,而不是我打算工作的方式。 web.config中的重定向似乎存在问题,但我一直无法弄清楚。

+1

你也可以发布你的观点吗? 也不要忘记删除这个'GlobalFilters.Filters.Add(new HandleErrorAttribute());' –

+1

你可以直接导航到你的404页面吗? –

+0

是的,我可以。这里的问题似乎源于web.config中的设置,尽管我找不到那里的错误。 – FLClover

回答

0

我得到它的工作。看来我对这个问题的理解从一开始就有些不对。

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

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

... 和 ...

<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> 

这直接重定向到的意见。我的理解是我不得不重定向到错误控制器,错误控制器又会重定向到视图,但显然情况并非如此。我想感谢你的评论,因为他们让我在再次分析问题的时候,我已经想要抛弃自定义错误的东西,只是懒惰地显示YSOD。 :)

2

还有一个问题与该设置,当你有更复杂的路线,并有几个段前。

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" } 
     ); 

希望它可以帮助别人后添加额外的路线:-)

+0

你是对的。我也会将它融入到我的路线中。谢谢! – FLClover

相关问题