相关疑难解决方法(0)

属性似乎根本不起作用

我在控制器操作上使用[HandleError]属性时遇到问题 - 它似乎根本不起作用(即过滤器是否存在并不重要 - 我得到相同的结果......).当抛出异常时,我在'/'应用程序错误页面而不是我的自定义视图中得到标准的红色服务器错误.

我在SO上找到了关于这个主题的几个其他线程,在大多数情况下,似乎在web.config中将customErrors选项设置为On解决了这个问题.它不适合我,所以我需要找到一个不同的解决方案.

我的控制器动作:

[HandleError]
public ActionResult Index()
{
    throw new Exception("oops...");
    return View();
}
Run Code Online (Sandbox Code Playgroud)

在我的web.config文件中

<customErrors mode="On"></customErrors>
Run Code Online (Sandbox Code Playgroud)

我确保Error.aspx文件也在共享目录中.我错过了什么?

我正在运行ASP.NET MVC RC Refresh.

error-handling asp.net-mvc attributes handleerror

11
推荐指数
1
解决办法
6874
查看次数

用try - catch包围的C#属性

我发现自己用try {stuff} catch(异常e){log,其他东西}编写方法稍微退出,所以我试图弄清楚如何创建一个属性来帮助我.我已经非常广泛地检查了以下线程,似乎无法让我的实现工作.

属性似乎根本不起作用

ASP.NET MVC Controller.OnException未被调用

.net处理异常的属性 - 在属性访问器上使用

我的顶级web.config设置为

<customErrors mode="On" defaultRedirect="/error.html"/>
Run Code Online (Sandbox Code Playgroud)

我正在编译非调试模式.我的属性如下:

    [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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我在这里叫它 -

public class Test : Controller
{
    [SafeWebMethod]
    public ActionResult Test()
    {
        throw new ArgumentException("test");
    }
}
Run Code Online (Sandbox Code Playgroud)

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

我也复制了[HandleError]属性中的代码,也无法在那里得到断点,所以我认为我的配置有问题,但我无法弄清楚是什么.

任何想法或想法将不胜感激

c# error-handling attributes

11
推荐指数
1
解决办法
4270
查看次数