Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用try-catch包围的C#属性_C#_Error Handling_Attributes - Fatal编程技术网

用try-catch包围的C#属性

用try-catch包围的C#属性,c#,error-handling,attributes,C#,Error Handling,Attributes,我发现自己编写了try{stuff}catch(异常e){log,other stuff}退出的方法,所以我试图找出如何创建一个属性来帮助我。我已经广泛地检查了以下线程,似乎无法使我的实现正常工作 我的顶级web.config设置为 <customErrors mode="On" defaultRedirect="/error.html"/> 我在这里叫它- 我似乎无法在属性中获得断点命中,或者如果我更改状态代码以使其显示 我还从[HandleError]属性复制了代码,也无

我发现自己编写了try{stuff}catch(异常e){log,other stuff}退出的方法,所以我试图找出如何创建一个属性来帮助我。我已经广泛地检查了以下线程,似乎无法使我的实现正常工作

我的顶级web.config设置为

<customErrors mode="On" defaultRedirect="/error.html"/>
我在这里叫它-

我似乎无法在属性中获得断点命中,或者如果我更改状态代码以使其显示

我还从[HandleError]属性复制了代码,也无法在其中获取断点,因此我认为我的配置有问题,但我不知道是什么原因


如果您能从您提供的链接中获得任何想法或想法,我们将不胜感激。

您想要的东西是不可能的(请参阅Aaronaught第二段代码片段中的实现)

除非您使用某种方面框架,否则您必须实现检查属性是否存在并自己对其进行操作的代码,并在每个
catch(…)
语句中运行该代码

您的想法很好,我可以自己在框架中使用它,但约束条件仍然是您必须自己实现它


Mario

hm,根据msdn的说法,HandleError“表示一个用于处理由动作方法引发的异常的属性”,我并没有看到任何关于属性访问器的严格规定。也就是说,可能是因为我的“方法”不是动作方法吗?我不喜欢asp web编程,但我认为动作方法与mvc控制器调用的代码隐藏方法相反。我进一步假设mvc控制器捕捉到异常并计算方法的属性,然后相应地进行处理。这是您也可以使用的模式-在调用方的catch(..)中,编写一个函数来检查属性并相应地执行操作。hth马里奥
    [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");
    }
}