سؤال

I want to capture site traffic in my ASP.NET MVC 4.0 website. I have done this

My Base Controller:

 public class BlogActionFilterAttribute : ActionFilterAttribute
        {
            public override void OnResultExecuting(ResultExecutingContext filterContext)
            {
                base.OnResultExecuting(filterContext);
                BaseModels.StoreSessionData(filterContext);
            }
        }

Global.ASAX:

protected void Application_Start()
        {

            // Register global filter
            GlobalFilters.Filters.Add(new BaseController.SiteActionFilterAttribute());
}

So far everything works pretty good.

if my action view has any Render Action or Action helper, then my session event is storing another new record in my db, this is because my Global filter is for ActionFilterAttribute.

If one page request has more than one action then, my session event will capture all those actions as events.

How can I restrict my filter to work on per-page-request but not Action and its sub actions?

هل كانت مفيدة؟

المحلول

If I understand you properly, you want to restrict the types of Actions for which you StoreSessionData. I assume one such type you want to restrict is ChildActions. If so, here is how you would do so"

 public class BlogActionFilterAttribute : ActionFilterAttribute
        {
            public override void OnResultExecuting(ResultExecutingContext filterContext)
            {
                base.OnResultExecuting(filterContext);
                if (!filterContext.IsChildAction)
                    BaseModels.StoreSessionData(filterContext);
            }
        }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top