Asp.net mvc 4 未触发MVC4自定义OnActionExecuting Global.asx筛选器

Asp.net mvc 4 未触发MVC4自定义OnActionExecuting Global.asx筛选器,asp.net-mvc-4,Asp.net Mvc 4,我正在尝试创建一个全局过滤器,如果用户登录,它将为我的每个操作运行。根据我所读到的,有两个步骤是必要的。首先,在Global.asx文件中添加新过滤器 public class MvcApplication : System.Web.HttpApplication { //I added this public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filte

我正在尝试创建一个全局过滤器,如果用户登录,它将为我的每个操作运行。根据我所读到的,有两个步骤是必要的。首先,在Global.asx文件中添加新过滤器

public class MvcApplication : System.Web.HttpApplication
{
    //I added this
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new NotificationFilter());
    }
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
    }
}
然后我必须在filters文件夹中创建过滤器本身

public class NotificationFilter : ActionFilterAttribute 
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    { //Breakpoint here is never triggered
        //This code doesn't work, but it's what I want to do
        if (WebSecurity.CurrentUserId > 0)
        {
            var notificationCount = db.Notifications.GroupBy(i => i.UserID).Count();
            if (notificationCount > 99)
            {
                ViewBag.Notifications = "99+";
            }
            else
            {
                ViewBag.Notifications = notificationCount;
            }
        }
        base.OnActionExecuting(filterContext);
    }
}

我怎样才能做到这一点?有更好的办法吗?我可以把它添加到所有的控制器中,而且效果很好,这还不够理想。

我也有过同样的经历。您可以构建BaseController类并将过滤器定义放入其中。然后必须从BaseController类继承所有控制器。所以您不必在所有控制器中都使用filter类

大概是这样的:

    public class BaseController : Controller
    {

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
         ...
         }
    }
在控制器中:

public class SampleController : BaseController
{
 ...
}

Jed,据我所知,您希望所有页面都以相同的方式运行,并为经过身份验证的用户提供通知

我建议,在您的_布局中,放置一个左上角的Div和jquery Ajax调用(稍后可以将jquery推送到alert.js文件中)

首先,我建议您进行一次此渲染以证明其功能,然后建议您进行一次定期调用


此堆栈线程将介绍如何使Ajax调用按时间间隔重复。

您可以使用以下代码解决问题:

public class ParentController : Controller
{

   protected override void OnActionExecuting(ActionExecutingContext filterContext)
   {
       /* take current action*/
       action = (string)filterContext.RouteData.Values["action"];

       /* check if your action have your filter */
       if (!string.IsNullOrEmpty(action) && Methods.
                    Where(
                           method => method.Name == action
                           &&        method.GetCustomAttributes(typeof(/* your filter name */), true).Length > 0
                    ).Count() > 0)
          {
            // do your work    
          }


   }
}


一种可能的方法是对推送通知使用小视图和信号器

在哪里创建BaseController?在过滤器、Global.asx文件或其他地方?很抱歉,这一切都是新的。不,这是一个单独的类,你可以把它放在任何地方。我会认为Action过滤器。这是更好的答案。问得好,杰德。为什么您需要为每个登录用户的每个页面执行一个操作?@Dave我想我需要一个操作,这样我就可以获得用户在任何页面上的通知数量,如果他们登录了。OIC。第二个问题:您希望在渲染时还是实时设置通知数?请注意,在堆栈或FB上,您可以看到您的通知在渲染后发生更改…理想情况下,它们在渲染后会发生更改,响应速度更快,但是,我想我应该从更简单的内容开始,因为我几乎不了解我在做什么。如果你能告诉我一些很棒的东西。
public JsonResult AjaxUserNotifications()
{

    Notifications Notifications = new GetNotifications();

    return Json(Notifications, JsonRequestBehavior.AllowGet);
}
public class ParentController : Controller
{

   protected override void OnActionExecuting(ActionExecutingContext filterContext)
   {
       /* take current action*/
       action = (string)filterContext.RouteData.Values["action"];

       /* check if your action have your filter */
       if (!string.IsNullOrEmpty(action) && Methods.
                    Where(
                           method => method.Name == action
                           &&        method.GetCustomAttributes(typeof(/* your filter name */), true).Length > 0
                    ).Count() > 0)
          {
            // do your work    
          }


   }
}
public class YourController : ParentController 
{

  // implementation of your controller
}