User tracking with ASP.NET MVC 3 and razor views

自闭症网瘾萝莉.ら 提交于 2019-11-30 11:38:54

问题


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 url on my site that a person visits in a database, but I'm not sure where to implement this code in ASP.NET MVC.


回答1:


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?




回答2:


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...



来源:https://stackoverflow.com/questions/6349842/user-tracking-with-asp-net-mvc-3-and-razor-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!