我发现自己写使用try {东西}赶上(例外五)方法{登录,其他的东西}退出了一点,所以我试图找出如何使一个属性,以帮助我出去了。我已经非常广泛地检查了以下线程,并且似乎无法让我的实现工作。C#属性与尝试包围 - 抓
attribute does not seem to act at all
ASP.NET MVC Controller.OnException not being called
.net Attributes that handle exceptions - usage on a property accessor
我的顶级web.config中设置为
<customErrors mode="On" defaultRedirect="/error.html"/>
,我在没有调试模式下进行编译。我的属性是如下:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class SafeWebMethodAttribute: ActionFilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
filterContext.ThrowIfNull();
if (filterContext.ExceptionHandled)
{
return;
}
Log.LogException(filterContext.Exception);
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.HttpContext.Response.Write(filterContext.Exception);
filterContext.ExceptionHandled = true;
}
}
,我在这里称之为 -
public class Test : Controller { [SafeWebMethod] public ActionResult Test() { throw new ArgumentException("test"); } }
我似乎无法得到的属性打一个断点,或者如果我改变状态码让它显示出来。
我也从[HandleError]属性中复制了代码,并且无法在那里获得断点,所以我认为这是我的配置有问题,但我无法弄清楚是什么。
任何想法或意见,将不胜感激
HM,根据MSDN,http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute.aspx,的HandleError“表示属性用于处理由操作方法抛出的异常“,而且我对于属性访问器没有任何严格的规定。话虽如此,可能是因为我的“方法”不是一种行动方法? – hermitt 2012-03-13 11:55:53
我没有进入ASP Web编程,但我认为动作方法颠倒了由mvc控制器调用的方法背后的代码。我进一步假设mvc控制器捕获异常并评估属性,如果该方法相应地执行。这也是你可以使用的模式 - 在调用者的catch(..)中写入一个函数来检查属性并相应地执行。马里奥 – 2012-03-16 05:22:22