C# 抛出异常ActionFilterAttribute OnActionExecuting

C# 抛出异常ActionFilterAttribute OnActionExecuting,c#,ajax,asp.net-mvc,actionfilterattribute,custom-action-filter,C#,Ajax,Asp.net Mvc,Actionfilterattribute,Custom Action Filter,我想在ActionFilterAttribute中抛出一个异常,如下所示: public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); if (!_controller.HasRoleIndex()) { throw new Exception(Resources.BackOffice.l

我想在ActionFilterAttribute中抛出一个异常,如下所示:

public override void OnActionExecuting(ActionExecutingContext filterContext) {
    base.OnActionExecuting(filterContext);

    if (!_controller.HasRoleIndex()) {
        throw new Exception(Resources.BackOffice.lblErrorPermission);
    }
}
我希望这样,因为我想在一些ajax请求中执行此代码,当用户没有权限时,发送一个异常,然后显示一条消息

我实现了在异常中显示消息的代码,它适用于应用程序其他位置的异常,但是当ActionFilterAttribute的OnActionExecuting中抛出异常时,应用程序中会出现未处理的异常,而我在视图中看到的是服务器错误

在不重定向到其他页面的情况下,如何处理异常?我只想抛出异常,不显示404错误页面

编辑:

把事情放在上下文中:我有一个带有一些ajax调用的网页,我希望ajax调用具有一种行为,以防在调用时发生错误,所以我的所有ajax中都有类似的内容:

 $.ajax({
 ...
 error: function (e, o, errorText) { AlertMessages.SetErrorMessageAndShow(errorText); },
 ...
});
此代码显示一个模式窗口,其中显示一条友好的用户消息,并说明错误原因: 会话已过期。糟糕的电子邮件地址等

我想使用相同的代码进行验证。通过验证,我考虑使用ActionFilterAttribute

但我无法从OnActionExecuting获取如何发送和异常


这可能吗?或者我必须返回一个特定的Json,然后在javascript中记录返回的Json?

看看这个答案似乎重定向到另一个页面,我不想导航到另一个页面。我希望控制器返回异常,而不是重定向到另一个页面。返回异常意味着什么?你必须向用户展示一些东西。我编辑了这个问题,试图解释我为什么要抛出异常。