未找到视图“错误”或其主控 - web.config未指定错误视图

时间:2021-02-23 18:54:51

Background: On a public facing ASP.NET MVC4 application, sometimes I receive inbound requests to a bad URL. The referrer is from outside so my app which is out of my control (I'm not generating the bad URL in my app). So MVC correctly raises an exception AND the user sees the custom error page. The global.asax is coded to email errors to me.

背景:在面向公众的ASP.NET MVC4应用程序中,有时我会收到对错误URL的入站请求。引荐来自外部所以我的应用程序是我无法控制的(我没有在我的应用程序中生成错误的URL)。因此MVC正确引发异常并且用户看到自定义错误页面。 global.asax被编码为电子邮件错误给我。

Problem. Although the URL is bad, the error I'm receiving is unexpected.

问题。虽然URL很糟糕,但我收到的错误是意外的。

Ex: - User navigates (from an external URL) to /Blog/View - The Blog controller does not have a View action - The user is presented with the Error500 custom error web page - The error I receive by email is:

例如: - 用户导航(从外部URL)到/ Blog / View - 博客控制器没有View操作 - 向用户显示Error500自定义错误网页 - 我通过电子邮件收到的错误是:

The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Blog/Error.aspx ~/Views/Blog/Error.ascx ~/Views/Shared/Error.aspx ~/Views/Shared/Error.ascx ~/Views/Blog/Error.cshtml ~/Views/Blog/Error.vbhtml ~/Views/Shared/Error.cshtml ~/Views/Shared/Error.vbhtml

未找到视图“错误”或其主页,或者没有视图引擎支持搜索的位置。搜索了以下位置:〜/ Views / Blog / Error.aspx~ / Views / Blog / Error.ascx~ / Views / Shared / Error.aspx~ / Views / Shared / Error.ascx~ / Views / Blog / Error。 cshtml~ / Views / Blog / Error.vbhtml~ / Views / Shared / Error.cshtml~ / Views / Shared / Error.vbhtml

I don't understand why ASP.NET MVC4 is looking for a view named "Error", and why MVC does not search for the view as specified (Error500) in the web.config. Here are the applicable source files:

我不明白为什么ASP.NET MVC4正在寻找名为“Error”的视图,以及为什么MVC不会在web.config中搜索指定的视图(Error500)。以下是适用的源文件:

The Web.Config:

<customErrors mode="RemoteOnly" defaultRedirect="~/Error/Error500">
  <error statusCode="404" redirect="~/Error/Error404" />
</customErrors>

The ErrorController file:

ErrorController文件:

   public class ErrorController : Controller
   {
       public ActionResult Error500()
       {
           return View();
       }

       public ActionResult Error404()
       {
          return View();
       }
   }

The Error404.cshtml file (located in the /Views/Error folder):

Error404.cshtml文件(位于/ Views / Error文件夹中):

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

   <h1>That's interesting</h1>
   <p>The page you were looking for could not be found.</p>

The Error500.cshtml file (located in the /Views/Error folder):

Error500.cshtml文件(位于/ Views / Error文件夹中):

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

<h1>We're sorry about that</h1>
<p>Something unexpected just happened.  Our IT staff has been notified...time to code a hot-fix!</p>

1 个解决方案

#1


2  

Are you sure that the user is hitting a URL that does not exist? It seems from a small repro that I just made that this works as desired if the action method does not exist. When an error is thrown inside an action method that was resolved by the routing and you are using the HandleErrorAttribute then you will get the error that you mention. What does your routing look like?

您确定用户正在访问不存在的URL吗?从一个小的repro看来,我刚刚提出,如果动作方法不存在,这可以按预期工作。如果在路由解析的操作方法中抛出错误并且您正在使用HandleErrorAttribute,那么您将收到您提到的错误。你的路由是什么样的?

As an aside emailing your self on error is not a sustainable route to receiving errors about your application! You should look into an error logging service to handle this for you. I recommend Bugsnag. (Disclaimer: I work at Bugsnag :))

另外,在发送错误时通过电子邮件发送自己并不是接收应用程序错误的可持续途径!您应该查看错误日志记录服务以便为您处理此问题。我推荐Bugsnag。 (免责声明:我在Bugsnag工作:))

#1


2  

Are you sure that the user is hitting a URL that does not exist? It seems from a small repro that I just made that this works as desired if the action method does not exist. When an error is thrown inside an action method that was resolved by the routing and you are using the HandleErrorAttribute then you will get the error that you mention. What does your routing look like?

您确定用户正在访问不存在的URL吗?从一个小的repro看来,我刚刚提出,如果动作方法不存在,这可以按预期工作。如果在路由解析的操作方法中抛出错误并且您正在使用HandleErrorAttribute,那么您将收到您提到的错误。你的路由是什么样的?

As an aside emailing your self on error is not a sustainable route to receiving errors about your application! You should look into an error logging service to handle this for you. I recommend Bugsnag. (Disclaimer: I work at Bugsnag :))

另外,在发送错误时通过电子邮件发送自己并不是接收应用程序错误的可持续途径!您应该查看错误日志记录服务以便为您处理此问题。我推荐Bugsnag。 (免责声明:我在Bugsnag工作:))