自定义错误页不显示,即使我设置CustomError ="在" [英] Custom Error Page does not show even though I set CustomError="On"

查看:220
本文介绍了自定义错误页不显示,即使我设置CustomError ="在"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio 2012中的MVC 4框架和希望看到该共享文件夹的友好的错误页面,如果异常未处理。 (我知道,作为一个develepor我需要看到错误的详细信息,但我只是想测试此设置)

到目前为止,我知道,在web.config文件的system.web节添加以下code应该做的伎俩

 <的customErrors模式=ON/>

不幸的是我,而不是友好的错误页面,我得到这样的:


  在/应用

服务器错误。


  
  

运行时错误说明:在出现应用程序错误
  服务器。此应用程序prevent当前自定义错误设置
  正在观看的应用程序错误的从细节。


  
  

详细信息:若要使此特定错误消息的详细信息是
  可见在本地服务器计算机上,请创建
  位于根下的web.config配置文件中的标签
  当前Web应用程序的目录。这个标签
  那么应该有它的mode属性设置为仅限远程。启用
  细节上远程计算机上查看,请设置模式
  关。


 <! -  web.config配置文件 - > <结构>
     <&的System.Web GT;
         <的customErrors模式=仅限远程/>
     < /system.web> < /结构>


  

注意:你所看到的当前错误页可以被取代
  通过修改的的defaultRedirect属性自定义错误页
  应用程序的配置标记指向自定义
  错误页的URL。


 <! -  web.config配置文件 - > <结构>
     <&的System.Web GT;
         <的customErrors模式=ON的defaultRedirect =mycustompage.htm/>
     < /system.web> < /结构>


我想做到这一点像加入HomeController的一个动作,设置路线或描述的东西,而不是一个解决方法。

这应该工作的参考如下:

<一个href=\"http://pluralsight.com/training/players/PSODPlayer?author=scott-allen&name=mvc3-building-controllers&mode=live&clip=0&course=aspdotnet-mvc3-intro\">http://pluralsight.com/training/players/PSODPlayer?author=scott-allen&name=mvc3-building-controllers&mode=live&clip=0&course=aspdotnet-mvc3-intro

(左边选择章节行动过滤器,然后跳到分钟3:30)




UPDATE(链接更改):

<一href=\"http://pluralsight.com/training/Player?author=scott-allen&name=mvc4-building-m2-controllers&mode=live&clip=0&course=mvc4-building\">http://pluralsight.com/training/Player?author=scott-allen&name=mvc4-building-m2-controllers&mode=live&clip=0&course=mvc4-building

(关于细线顶一下,指出控制器在ASP.NET MVC 4,然后单击筛选器操作,然后跳到分钟5:00)




是否有人有线索,为什么我不能看到友好的错误页面,上面提到的设置?

在此先感谢!


解决方案

下面是一步一步的指导,这样做,

要开始,首先设置的customErrors的mode属性为开。 (这指定自定义错误已启用。)

1。从Web.config文件code

 &LT;&的System.Web GT;    &LT;的customErrors模式=ON&GT;&LT; /&的customErrors GT;&LT; /system.web>

下面是我的控制器code,我指定的控制器类的HandleError属性。也产生我从Index操作抛出一个异常错误。

2。从HomeController.cs code

  [的HandleError]
公共类HomeController的:控制器
{
    公众的ActionResult指数()
    {
    抛出新的异常(错误!);
    ViewBag.Message =欢迎使用ASP.NET MVC!;    返回查看();
    }    公众的ActionResult关于()
    {
    返回查看();
    }
}

正如你可以在下面的截图中看到,我有意见的文件夹的共享文件夹中定义的视图Error.cshtml

3。 Solution Explorer中

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

模型

4。从Error.cshtml code

  @model System.Web.Mvc.HandleErrorInfo@ {
    ViewBag.Title =错误;
}&LT; H2&GT;
    很抱歉,在执行您的要求时发生了一个错误。
&LT; / H&GT;

在运行的项目,而不是让黄死亡屏现在我得到以下输出。

这篇文章

更新:

试试这个通过创建一个新的项目,这应该和不工作。我怀疑你的 Error.cshtml 文件可能不具有模型 @model System.Web.Mvc.HandleErrorInfo 或你可以是具有不同的查看文件夹的多个页面Error.cshtml

I am using Visual Studio 2012 with the MVC 4 Framework and would like to see the friendly error page of the shared folder, if an exception is unhandled. (I know, as a develepor I need to see the error details but I just wanted to test this setting)
So far I know that adding the following code in the system.web section of web.config should do the trick

<customErrors mode="On" />

Unfortunately instead of my friendly error page I get this:

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>


I would like to do it as described and not with a workaround like adding an action to HomeController, setting routes or something.
A reference that this should work is the following:
http://pluralsight.com/training/players/PSODPlayer?author=scott-allen&name=mvc3-building-controllers&mode=live&clip=0&course=aspdotnet-mvc3-intro
(On left select the chapter "Action Filters" and skip to minute 3:30)


UPDATE (link changed):
http://pluralsight.com/training/Player?author=scott-allen&name=mvc4-building-m2-controllers&mode=live&clip=0&course=mvc4-building
(On top click on the thin line that states "Controller in ASP.NET MVC 4", then click on "Action Filters" and skip to minute 5:00)

Does anybody have a clue why I cannot see the friendly error page with the setting mentioned above?
Thanks in advance!

解决方案

Here is step by step guide to doing this,

To start with, first set the "customErrors’s" mode property to ‘on’. (This specifies that custom errors are enabled.)

1. Code from Web.config

<system.web>

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

</system.web>

Below is my controller code, I have specified the HandleError attribute on the controller class. Also to generate an error I am throwing an exception from the Index action.

2. Code from 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();
    }
}

As you can see in the screenshot below, I have a Error.cshtml view defined inside the Shared folder of the Views folder.

3. Solution Explorer

Note that the Error.cshtml view (shown below) uses a model of ‘System.Web.Mvc.HandleErrorInfo’

4. Code from Error.cshtml

@model System.Web.Mvc.HandleErrorInfo

@{
    ViewBag.Title = "Error";
}

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

On running the project, instead of getting the yellow death screen now I get the following output.

Taken from this article

Update :

Try this out by creating a new project, this should and does work. I suspect that your Error.cshtml file may not have model as @model System.Web.Mvc.HandleErrorInfo or you may be having multiple Error.cshtml pages in different view folders.

这篇关于自定义错误页不显示,即使我设置CustomError =&QUOT;在&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆