使用 ASP.NET MVC 3 和 razor 视图进行用户跟踪 [英] User tracking with ASP.NET MVC 3 and razor views

查看:29
本文介绍了使用 ASP.NET MVC 3 和 razor 视图进行用户跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ASP.NET MVC 3 中使用 Razor 视图时,在整个网站上实现用户跟踪的最佳方法是什么.

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

在网络表单中,我会在母版页中放置一些代码以使用 cookie 并在我的网站上记录一个人在数据库中访问的每个 url,但我不确定在 ASP.NET MVC 中的何处实现此代码.

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.

推荐答案

我想最好的方法是创建一个全局操作过滤器,并跟踪那里的访问.

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

在 global asax 中将其注册为全局过滤器:

Register it as a global filter in global asax:

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

    RegisterGlobalFilters(GlobalFilters.Filters);
}

仅此而已.好看吗?

这篇关于使用 ASP.NET MVC 3 和 razor 视图进行用户跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆