如何捕获 Webmatrix / Razor / ASP.NET 网页中的唯一用户会话?
我需要在 Webmatrix / Razor / ASP.NET 网页中记录唯一的用户会话。 _appstart 是在应用程序第一次在 IIS 中启动时触发,还是在每个唯一用户点击时触发一次?如果只有一次,我如何捕获独特的用户会话和信息?设置?
更新:我不确定 Global.asax 事件是否在 Razor / ASP.NET 网页下触发。我对其进行了测试,Session_Start 事件触发得很好。问题解决了。
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Dictionary<DateTime, String> d = new Dictionary<DateTime, String>();
Application.Lock();
if (Application["d"] != null)
{
d = (Dictionary<DateTime, String>)Application["d"];
}
d.Add(DateTime.Now, HttpContext.Current.Session.SessionID);
Application["d"] = d;
Application.UnLock();
}
I need to log unique user sessions in Webmatrix / Razor / ASP.NET Web Pages. Does _appstart fire just when the app spins up the first time in IIS or does it fire once per unique user hit? If just once, how do I capture unique user sessions & settings?
UPDATE: I wasn't sure if the Global.asax events were fired under Razor / ASP.NET WebPages. I tested it out and the Session_Start event fires just fine. Question resolved.
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Dictionary<DateTime, String> d = new Dictionary<DateTime, String>();
Application.Lock();
if (Application["d"] != null)
{
d = (Dictionary<DateTime, String>)Application["d"];
}
d.Add(DateTime.Now, HttpContext.Current.Session.SessionID);
Application["d"] = d;
Application.UnLock();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了直接回答您的问题,_AppStart 在第一个用户访问您的网站时运行。该站点的未来用户不会导致 _AppStart 运行。没有特定的页面或位置来放置为每个唯一用户运行的代码。
您想要做的是查看 ASP.Net Session 对象。在您的页面中,您可以像这样存储和检索会话中的数据:
ASP.Net 将负责确保使用会话 Cookie 为每个浏览器实例存储设置。请注意,如果您处于 Web Farm 环境中,则需要更强大的东西,但是当您谈论单个服务器时,这应该没问题。
如果您想了解更多信息,请参阅 ASP.Net 会话状态的官方文档:http ://msdn.microsoft.com/en-us/library/ms178581.aspx
To directly answer your question, _AppStart runs when the first user hits your site. Future users to the site do NOT cause _AppStart to run. There is no specific page or place to put code that runs for each unique user.
What you want to do is take a look at the ASP.Net Session object. In your page, you can store and retrieve data from Session like so:
ASP.Net will take care of making sure that the setting is stored per-browser-instance using Session Cookies. Note that if you're in a Web Farm environment, you'll need something more robust, but when you're talking about a single server, this should be fine.
If you want some more info, here's the official documentation for ASP.Net Session State: http://msdn.microsoft.com/en-us/library/ms178581.aspx
您询问了有关记录“唯一用户会话”的问题,这有点令人困惑。所有会话都是唯一的,但并非所有会话都属于唯一访客。返回的访客将开始新的会话。如果您想保留会话计数,可以挂钩 Global.asax 中的 Session_Start 事件。如果您想计算唯一访客数,请使用 cookie。如果用户还没有 cookie,请在用户访问时设置它们。确保它们的到期时间是在未来的某个时间。如果访问者没有获得您网站的跟踪 cookie,那么他们一定是新访问者(或者他们可能已经删除了他们的 cookie...)
You have asked about logging "unique user sessions", which is a little confusing. All sessions are unique, but not all sessions belong to unique visitors. Returning visitors will start new sessions. If you want to keep a count of sessions, you can hook into the Session_Start event in Global.asax. If you want to count unique visitors, use cookies. Set them when a user visits if one hasn't already got a cookie. Ensure that their expiry is some time well into the future. If the visitor hasn't got a tracking cookie for your site, they must be new (or they might have deleted their cookie...)