User tracking with ASP.NET MVC 3 and razor views

前端 未结 2 1767
夕颜
夕颜 2020-12-25 09:35

What\'s the best way to implement user tracking throughout your web site when using Razor views in ASP.NET MVC 3.

In webforms I\'d put some code in the masterpage to

相关标签:
2条回答
  • 2020-12-25 10:03

    I guess the best way to do this is to create a Global Action Filter, and track visits there.

    Create an action filter attribute:

    public class UserTrackingActionFilterAttribute : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext context)
        {
            base.OnResultExecuting(context);
    
            //save url, userId from session, etc...
        }
    }
    

    Register it as a global filter in global asax:

    protected void Application_Start()
    {      
        // Register global filter
        GlobalFilters.Filters.Add(new UserTrackingActionFilterAttribute());
    
        RegisterGlobalFilters(GlobalFilters.Filters);
    }
    

    That's all. Nice?

    0 讨论(0)
  • 2020-12-25 10:19

    I wouldn't do any of it with Razor views.

    You will want to build an ActionFilter and attach it as a GlobalFilter. Let it do all the work for you.

    More good reading...

    0 讨论(0)
提交回复
热议问题