Track # of page viewed in a session in asp.net
I am currently working on a project where I want to implement a bit of logic for each .aspx viewed.
My idea was to use an httphandler that will target *.aspx, and in the handler, I would do my bit of logic, such as printing out: This is the xth page you have visited in this session.
I am curious if there are any problems with my idea or is there a more proper solution I am not aware of.
Though I have tried implementing my solution, I run into an infinite loop. After I complete my logic with the 开发者_高级运维handler, I redirect to the same page, but that of course calls the same handler. Is there a way to bypass the handler on the redirect or a specific way to execute the same page without accessing the handler.
Thanks for the help! ~
Are you using a master page? If so, you could simply increase the count of your session variable in the master page's load event and display or write to DB anytime thereafter.
I think possibly you mean an HttpModule - a handler is an endpoint, you wouldn't have much of a page to display as well.
I would have suggested you add such a counter to a root master page. Simple to implement, doesn't have pipeline complications, can encapsulate the output control a well as the logic behind it.
Your handler shouldn't redirect...
Create an HttpModule and put this in the request pipeline. That way your code will execute and add the info you want while letting the page itself execute as necessary.
Or you could just take Matt's idea and put this in the master page. You'll have more control over where your text goes that way AND it's way simpler to implement.
You should do this with an IHttpModule
instead. Modules are executed once per request, and their purpose is to something that does not have to do with the rendering of a page, unlike a handler.
精彩评论