开发者

User tracking with ASP.NET MVC 3 and razor views

开发者 https://www.devze.com 2023-03-13 11:48 出处:网络
What\'s the best way to implement user tracking throughout your web site when using Razor views in ASP.NET MVC 3.

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 use a cookie and log each 开发者_如何学Curl on my site that a person visits in a database, but I'm not sure where to implement this code in ASP.NET MVC.


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?


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号