使用 ASP.NET MVC 3 和 Razor 视图进行用户跟踪

发布于 2024-11-15 10:54:09 字数 158 浏览 2 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫尐诺 2024-11-22 10:54:09

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

创建一个动作过滤器属性:

public class UserTrackingActionFilterAttribute : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext context)
    {
        base.OnResultExecuting(context);

        //save url, userId from session, etc...
    }
}

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

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

    RegisterGlobalFilters(GlobalFilters.Filters);
}

仅此而已。好的?

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?

安静 2024-11-22 10:54:09

我不会用 Razor 视图做任何事情。

您将需要构建一个 ActionFilter 并将其附加为 全局过滤器 。让它为您完成所有工作。

更多好书...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文