如何在[handleerror]停止工作后再次启用它? [英] How do I enable [handleerror] to work again after it has stopped working?

查看:100
本文介绍了如何在[handleerror]停止工作后再次启用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VS2015中有一个相当大的MVC5应用程序,可以访问数据库中大约25个表。我差不多完成它了,我正在尝试使用HandleException设置异常处理。



不幸的是,我没有意识到如果你试验它,移动投掷语句和HandleError属性,关闭和打开customErrors元素,删除(重新标记)和重新启用RegisterGlobalFilters,程序进入一个状态,无论你做什么,你都不能再使用HandleError进行异常处理。我希望我错了,并没有在我的应用程序中永久禁用HandleError属性。它忽略了我的HandleError属性并显示未处理的错误页面。



除了我的大型生产应用程序,我有两个小测试应用程序,其中一个HandleError属性正在运行完美和另一个同样也取消了其HandleError许可证(演示)。



以下是我的小应用程序中的代码失败并显示未处理的错误ArithmeticException和DataException错误。什么想法可能有什么问题?



我尝试过的事情:



Web.config

< system.web> 
< customErrors mode =On>

< authentication mode =None/>
< compilation debug =truetargetFramework =4.5.2/>
< httpRuntime targetFramework =4.5.2/>



HomeController.cs

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Mvc;
使用 System.Data;

namespace TestExceptionHandling.Controllers
{
[HandleError(ExceptionType = typeof (ArithmeticException),View = ArithmeticException)]
public class HomeController:Controller
{
public ActionResult Index()
{
return View();
}

[HandleError(ExceptionType = typeof (DataMisalignedException),View = DataMisaligned)]
public ActionResult About()
{
// 抛出新的DataMisalignedException();
throw new ArithmeticException( 糟糕,发生算术错误。);
ViewBag.Message = 您的应用程序描述页面。;

return 查看();
}

[HandleError(ExceptionType = typeof (DataException),View = DataException)]
public ActionResult Contact()
{
throw new DataException( 糟糕,发生数据异常错误。);
ViewBag.Message = 您的联系页面。;

return 查看();
}
}
}

ArithmeticException.cshtml


@ {
ViewBag.Title = ErrorView01;
布局= 〜/ Views / Shared / _LoginPartial.cshtml;
}

< h4> ErrorView01< h4>

< h4>发生算术异常。< / h4 < span class =code-keyword>>





DataMisaligned.cshtml

 @ {
ViewBag.Title =DataMisaligned;
Layout =〜/ Views / Shared / _LoginPartial.cshtml;
}

DataMisaligned.cshtml

< h2>由于数据错位问题,您的操作无法完成。< / h2>



DataException.cshtml

 {
ViewBag.Title =DataException;
Layout =〜/ Views / Shared / _LoginPartial.cshtml;
}

DataException.cshtml

< h2>由于数据异常,无法完成操作。如果删除数据对象,请执行DOT,确保所有从属对象,例如。通知,信件等首先单独删除。这是维护参照完整性和避免孤立数据库对象所必需的。< / h2>



所有上述视图都在〜/ Views / Shared



FilterConfig.cs

 命名空间 TestExceptionHandling 
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection)过滤器)
{
filters.Add( new HandleErrorAttribute());
}
}
}

解决方案

我找到了解决方案。当我为各种特定异常类型创建视图时,我指定它应该使用_Layout.cshtml进行布局。我发现这会导致异常。所以现在我没有选中使用布局页面框。我的[HandleError]功能正常工作。



戴夫,你是对的;我只需要找出这样的例外可能来自哪里。感谢。

I have a sizeable MVC5 application in VS2015 that accesses around 25 tables in the database. I'm nearly finished with it and am trying to set up exception handling using HandleException.

Unfortunately, I didn't realize that if you experiment with it, moving throw statements and HandleError attributes around, turning the customErrors element off and on, and removing (remarking) and re-enabling RegisterGlobalFilters, the program gets into a state where no matter what you do, you can never get the exception handling with HandleError working again. I am hoping I am wrong and have not permanently disabled HandleError attributes in my application. It is ignoring my HandleError attributes and displaying unhandled error pages.

Aside from my large production app, I have two small test apps, one in which the HandleError attribute is working perfectly and another which has also similarly gotten its HandleError license revoked (figure of speech).

Below is code from my small app that is failing and showing unhandled errors for the ArithmeticException and the DataException errors. Any ideas what could be wrong?

What I have tried:

Web.config

<system.web>
	<customErrors mode="On">

	<authentication mode="None" />
	<compilation debug="true" targetFramework="4.5.2" />
	<httpRuntime targetFramework="4.5.2" />


HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;

namespace TestExceptionHandling.Controllers
{
    [HandleError(ExceptionType = typeof(ArithmeticException), View = "ArithmeticException")]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HandleError(ExceptionType = typeof(DataMisalignedException), View = "DataMisaligned")]
        public ActionResult About()
        {
            //throw new DataMisalignedException();
            throw new ArithmeticException("Oops, an arithmetic error occurred.");
            ViewBag.Message = "Your application description page.";

            return View();
        }

        [HandleError(ExceptionType = typeof(DataException), View = "DataException")]
        public ActionResult Contact()
        {
            throw new DataException("Oops, a data exception error occurred.");
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

ArithmeticException.cshtml


@{
    ViewBag.Title = "ErrorView01";
    Layout = "~/Views/Shared/_LoginPartial.cshtml";
}

<h4>ErrorView01<h4>

<h4>An arithmetic exception occurred.</h4>



DataMisaligned.cshtml

@{
    ViewBag.Title = "DataMisaligned";
    Layout = "~/Views/Shared/_LoginPartial.cshtml";
}

DataMisaligned.cshtml

<h2>Your action could not be completed due to a data misalignment issue.</h2>


DataException.cshtml

{
    ViewBag.Title = "DataException";
    Layout = "~/Views/Shared/_LoginPartial.cshtml";
}

DataException.cshtml

<h2>The action could not be completed due to a Data Exception.  If deleting a data object, e'g' a DOT, make sure all subordinate objects, .e.g. notices, letters, etc. are individually deleted first.  This is required to maintain referential integrity and avoid orphaned database objects.</h2>


All of the above views are under ~/Views/Shared

FilterConfig.cs

namespace TestExceptionHandling
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
    }
}

解决方案

I found the solution. When I was creating the views for the various specific exception types, I was specifying that it should use _Layout.cshtml for layout. I discovered that that causes an exception. So now I am not checking the "Use a Layout Page" box. My [HandleError] functionality is working.

Dave, you were right; I just needed to find out where such an exception might come from. Thanks.


这篇关于如何在[handleerror]停止工作后再次启用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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