如何让 MVC3 给出更少的错误信息

发布于 2025-01-08 06:59:06 字数 3581 浏览 1 评论 0原文

默认情况下,MVC3 应用程序会给出大量 404、无效动词、未找到路由等信息。它有一个极其详细的错误页面。

我们如何改变这种行为,以便客户端仍然得到 404 和其他任何内容,但不会返回此类内容? (特别是底部的东西,它显示我们的私有框架信息、类名等。)

<html>
    <head>
        <title>The resource cannot be found.</title>
        <style>
         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 
         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
         pre {font-family:"Lucida Console";font-size: .9em}
         .marker {font-weight: bold; color: black;text-decoration: none;}
         .version {color: gray;}
         .error {margin-bottom: 10px;}
         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
        </style>
    </head>

    <body bgcolor="white">

            <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>

            <h2> <i>The resource cannot be found.</i> </h2></span>

            <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">

            <b> Description: </b>HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. &nbsp;Please review the following URL and make sure that it is spelled correctly.
            <br><br>

            <b> Requested URL: </b>/Foo<br><br>

            <hr width=100% size=1 color=silver>

            <b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.225

            </font>

    </body>
</html>
<!-- 
[HttpException]: A public action method &#39;Index&#39; was not found on controller &#39;MySystem.Api.Rest.Controllers.MyController&#39;.
   at System.Web.Mvc.Controller.HandleUnknownAction(String actionName)
   at System.Web.Mvc.Controller.ExecuteCore()
   at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
   at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d()
   at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f)
   at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
   at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
-->

By default, an MVC3 application gives out a LOT of information for 404's, invalid verbs, route not found, etc. It has this extremely verbose error page.

How can we change this behavior, so that the client still gets the 404's and whatever else, but without this sort of thing being returned?
(ESPECIALLY that stuff at the bottom, which displays our private framework information, class names, etc.)

<html>
    <head>
        <title>The resource cannot be found.</title>
        <style>
         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 
         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
         pre {font-family:"Lucida Console";font-size: .9em}
         .marker {font-weight: bold; color: black;text-decoration: none;}
         .version {color: gray;}
         .error {margin-bottom: 10px;}
         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
        </style>
    </head>

    <body bgcolor="white">

            <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>

            <h2> <i>The resource cannot be found.</i> </h2></span>

            <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">

            <b> Description: </b>HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.
            <br><br>

            <b> Requested URL: </b>/Foo<br><br>

            <hr width=100% size=1 color=silver>

            <b>Version Information:</b> Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.225

            </font>

    </body>
</html>
<!-- 
[HttpException]: A public action method 'Index' was not found on controller 'MySystem.Api.Rest.Controllers.MyController'.
   at System.Web.Mvc.Controller.HandleUnknownAction(String actionName)
   at System.Web.Mvc.Controller.ExecuteCore()
   at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
   at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d()
   at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f)
   at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
   at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
-->

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩起发的微风 2025-01-15 06:59:06

请参阅此处的步骤 9 打开零售模式。

默认 MVC 模板已启用自定义错误。

实现这一点的一种方法是启用零售模式。这将为您计算机上的每个站点启用自定义错误。

https://kevww.wordpress.com/tag/mvc/

基本上只需进入您的计算机即可.config 并将其设置在那里。
(例如 c:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config)

<system.web>  <deployment retail="true" /></system.web>

还要安装 ELMAH 以自动捕获您的错误(记录它们)。

http://code.google.com/p/elmah/wiki/MVC

See step 9 here to turn on retail mode.

The default MVC template has custom errors turned on.

A nie way to enable this is enabling retail mode. This will enable custom errors for EVERY site on your machine.

https://kevww.wordpress.com/tag/mvc/

Basically just go into your machine.config and set it there.
(ex c:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config)

<system.web>  <deployment retail="true" /></system.web>

Also install ELMAH to capture your errors (log them) automatically as well.

http://code.google.com/p/elmah/wiki/MVC

囚你心 2025-01-15 06:59:06

您可以在 web.config 中设置compilation debug="false"

如果您希望在 iis 中托管的所有网站都这样做,则可以在 machine.config 中设置 retail mode=true

如果您想显示自定义错误页面,则可以覆盖 HttpErrors web.config 中的 code> 部分。

You can set compilition debug="false" in web.config.

If you want this for all the websites hosted in iis, then you can set retail mode=true in machine.config

If you want to show a custom error page, you can override HttpErrors Section in web.config.

爱本泡沫多脆弱 2025-01-15 06:59:06

覆盖控制器中的 OnException 方法,并在错误时向用户显示您想要的任何内容(包括您的自定义页面样式等)。

例如,请参阅 http://blog.dantup.com /2009/04/aspnet-mvc-handleerror-attribute-custom.html

Override OnException method in your controllers and show whatever you want to user on error (including e.g. your custom page style etc).

For example, see http://blog.dantup.com/2009/04/aspnet-mvc-handleerror-attribute-custom.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文