加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 即使我设置CustomError =“On”,自定义错误页面

发布时间:2020-12-15 22:28:47 所属栏目:asp.Net 来源:网络整理
导读:我正在使用Visual Studio 2012和MVC 4 Framework,如果未处理异常,我希望看到共享文件夹的友好错误页面. (我知道,作为一个开发者,我需要查看错误详细信息,但我只想测试此设置) 到目前为止,我知道在web.config的system.web部分添加以下代码应该可以解决问题 cu
我正在使用Visual Studio 2012和MVC 4 Framework,如果未处理异常,我希望看到共享文件夹的友好错误页面. (我知道,作为一个开发者,我需要查看错误详细信息,但我只想测试此设置)
到目前为止,我知道在web.config的system.web部分添加以下代码应该可以解决问题
<customErrors mode="On" />

不幸的是,我得到了这个:

Server Error in ‘/’ Application.

Runtime Error Description: An application error occurred on the
server. The current custom error settings for this application prevent
the details of the application error from being viewed.

Details: To enable the details of this specific error message to be
viewable on the local server machine,please create a
tag within a “web.config” configuration file located in the root
directory of the current web application. This tag
should then have its “mode” attribute set to “RemoteOnly”. To enable
the details to be viewable on remote machines,please set “mode” to
“Off”.

<!-- Web.Config Configuration File -->

 <configuration>
     <system.web>
         <customErrors mode="RemoteOnly"/>
     </system.web> </configuration>

Notes: The current error page you are seeing can be replaced by a
custom error page by modifying the “defaultRedirect” attribute of the
application’s configuration tag to point to a custom
error page URL.

<!-- Web.Config Configuration File -->

 <configuration>
     <system.web>
         <customErrors mode="On" defaultRedirect="mycustompage.htm"/>
     </system.web> </configuration>

我想按照描述执行此操作,而不是使用向HomeController添加操作,设置路由或其他内容的解决方法.
这应该起作用的参考如下:
http://pluralsight.com/training/players/PSODPlayer?author=scott-allen&name=mvc3-building-controllers&mode=live&clip=0&course=aspdotnet-mvc3-intro
(在左侧选择“动作过滤器”一章并跳至分钟3:30)

更新(链接已更改):
http://pluralsight.com/training/Player?author=scott-allen&name=mvc4-building-m2-controllers&mode=live&clip=0&course=mvc4-building
(在顶部单击描述“ASP.NET MVC 4中的Controller”的细线,然后单击“Action Filters”并跳到分钟5:00)

有没有人知道为什么我看不到上面提到的设置的友好错误页面?
提前致谢!

解决方法

这是一步一步指导这样做,

首先,首先将“customErrors”模式属性设置为“on”. (这指定启用自定义错误.)

1.来自Web.config的代码

<system.web>

    <customErrors mode="On"></customErrors>

</system.web>

下面是我的控制器代码,我在控制器类上指定了HandleError属性.同样为了生成错误,我从Index操作中抛出异常.

2.来自HomeController.cs的代码

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
    throw new Exception("Error Occured !");
    ViewBag.Message = "Welcome to ASP.NET MVC!";

    return View();
    }

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

正如您在下面的屏幕截图中看到的,我在Views文件夹的Shared文件夹中定义了一个Error.cshtml视图.

3.解决方案Explorer

请注意,Error.cshtml视图(如下所示)使用’System.Web.Mvc.HandleErrorInfo’模型

4.来自Error.cshtml的代码

@model System.Web.Mvc.HandleErrorInfo

@{
    ViewBag.Title = "Error";
}

<h2>
    Sorry,an error occurred while processing your request.
</h2>

在运行项目时,我现在获得以下输出,而不是获得黄色死亡屏幕.

取自this article

更新:

通过创建一个新项目来尝试这个,这应该并且确实有效.我怀疑您的Error.cshtml文件可能没有@model System.Web.Mvc.HandleErrorInfo的模型,或者您可能在不同的视图文件夹中有多个Error.cshtml页面.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读