Hit counter for MVC ASP.NET website - c#

How to create a hit counter(visitor counter) for each page in MVC website?
And store information in MS SQL Database. The same as stackoverflow using on each page.
Please show me an example. Thank u

In your master page insert a function call that logs the name of the page or whatever other details you want. Run your counts and there you go.

I was able to do this using Session variables, sql server db in ASP.NET MVC.
Note: I used session variables instead of storing client IP address. As saving all the client address will overflow your database. This works similar to the Stackoverflow pages viewed property showing for every question at the top right.
This is how it worked for me.
Whenever a user opens a particular web page in your website, the counter increments and the updated count will be updated in sql server database(it could be anyother database) tables.
If the same user visits the same webpage again within the same session, the counter will not be incrmented. That means if user refreshes a web page multiple times with in the same session, the counter will not be incremented, as it is a single visit by that user.
If the same visitor closes his session(say he closes the browser) and reopens the same web page the counter will be incremented considering it as a separate visit.
Here are the steps I followed to implement it.
Have a method which will be automatically called before every Action method.
Inside this method, initiate a session if it is not done already and check if the user has visited this page already.
Now, update the counter variable accordingly and save the count to db if required.
Here is the complete procedure I have followed. Hope it helps.
Source: Count number of visitors of a page in asp.net mvc (HIT counter)

Related

Is there any way to store view state of a first page when user want to back from second one?

I have two pages.
On first user want to filter some data. After that he pick some value and go to second page.
On second page he will do some tasks and then he want to get back to first page to pick some other value to do other tasks.
But for now he have to filter back some data (on first page), but I want call back his filters. Is there any way to do this?
You got a few options here for this.
As STT mentioned in his comment to save the filter data in a session variable/ViewState, works all fine when you only got one web server otherwise you need to setup a shared sessions cache etc. The date will be kept temporary in the Server.
You can also save the data to a cookie and when loading page 1 reload last used filter used from the cookie. Then you got control of the expiry and session data is stored in Web Browser.
By adding the data for the filter into an URI parameter you can from page 2 include the data when user clicks a back button (not the browsers back button) The advantage of this its not session sensitive and the user can bookmark the url to get same filter later on.

Can one use sql query in default.aspx web application

Can I use sql query and alter server url based on the sql query result?
If sql query gives value of 1, point the server to home.aspx and if the sql query gives value of 0, point to home2.aspx?
I am trying to show different person different homepage based on a specific associated value in sql database.
Thank you
p.s. I honestly dont know how the log in works. I am not a .net guy and didn't create the system. I know about URL direct for the server in default.aspx because I have been through that. there is singlesignon settings applied on the website so when a user logs into the lets say web A, he/she can click on a hyperlink, it will pass the encrypted user login info and the user will automatically be on the home page of website b (log in page for website B is skipped). I will update my acceptance rate.
Thank you
p.s. 2: I am just trying to pull the INCOMING user's active flag from a column in a table in a sql db. If the flag for that particular user is set to 1, point them to URL A, if flag is set to 0, point them to URL B.
Thank you
You can redirect doing that but you should not! Why? Well, calling the DB every time a visitor comes will make your response time way long to wait. What you can do is cache the result on memory or at least disk (using a B+ tree for example) or better to provide a common homepage and then redirect.
Now, once you identify a user set say a cookie for the redirection and for so on use the cookie (don't forget it to authorize if needed), that'll speed up things.
Go ahead and take a look to personalization and user profiles on ASP.NET which is what you want to do.
Randolf's comments are true. But in order to do what you want, if you for some reason still want to do this, you'll want to look at Server.Transfer() (more efficient, and preferable if you are redirecting to another page that's on the same server, within the same app), or Response.Redirect().

how to capture the mouse idle state in Master Page Iframe in javascript

I am using a Master page and many other pages from different folders are displayed in the IFrame within the master page.
If the user is not doing anything for some time [5 minutes or so] in any of the other pages, I want to give an alert message either to continue or to log out.
How to do this?
For example setTimout in jsvascript on mouseover for your iFrame. On each mouseover drop your timeout with clearTimeout and create new one.
But I don't think it's a good idea. Better to handle session timeout on the server. More info about sessions state you can find here: ASP.NET Session State
After session ending user might me autmatically logged of and will be requested to login next time on any action whitch post request to the server.

Session Variable being created and access before long process?

I have a page that executes a long process, parsing over 6 million rows from several csv files into my database.
Question is just as when the user clicks "GO" to start processing and parsing 6 million rows I would like to set a Session Variable that is immediately available to the rest of my web site application so that any user of the web site knows that a user with a unique ID number has started parsing files without having to wait until the end of the 6 million rows processed?
Also with jQuery and JSON, I'd like to get feedback on a webpage as to which csv file is being processed and how many rows have been processed.
There could be other people parsing files at the same time, how could I track all of this and stop any mix up etc with other users even though there is no login or user authentication on the site?
I'm developing in C# with .NET 4.0 Entity Framework 4.0, jQuery, and MS SQL 2008 R2.
I was thinking of using Session Variables however in my static [WebMethod] for my jQuery JSON calls I am not able to pull back my Session unless I'm using HttpContext.Current.Session but I am not sure how if this solution would work?
Any guidance or ideas would be mostly appreciated.
Thanks
First of all: Session variables are not supposed to be seen for any user everywhere.
when some client connects to the server, there is a session made for them by the server, and next time the same user requests (within the expiration time of the session), the session (and it's variables) are usable.
You can use a static class for this if you intend to.
for example
public static class MyApplicationStateBag
{
public static Dictionary<string,object> Objects {get; private set;}
}
and for your progress report. you can use a asp:Timer to check the progress percentage every second or two.
here is a sample code that I have written for asp:Timer within an UpdatePanel:
Writing a code trigger for the updatepanel.
I suggest you use a Guid for identifying the current progress as the key to your state bag.
The correct way of doing this is via services, for example WCF services. You don't want to put immense load on the web server, which is not supposed to do that.
The usual scenario:
User clicks on GO button
Web server creates a job and starts this job on a separate WCF service
Each job has ID and metadata (status, start time, etc.) that is persisted to the storage
Web server returns response with job ID to the user
User, via AJAX (JQuery) queries the job in the storage, once completed you can retrieve results
You can also save Job ID to the session
P.S. it's not a direct answer to your question, but I hope it helps

Shopping Cart Problem

I have implemented simple shopping cart in a website. I save the cart and items in Session object. After user checkouts (using Paypal), I clear the cart items. This works fine, but I have seen a problem in following scenario:
Suppose a user added some items to cart and opened another browser and logged in. He can now see the items in both browsers. Now if he checks out in one browser he is still able to see the cart items in other window as both browser have separate sessions.
What approach should I adopt to avoid this or will have to live with this?
EDIT:- After posting the question I was thinking about it. I will settle for this simple solution, Whenever user goes for checkout, I will hit the database to load the cart instead of session. This way I won't be hitting database for showing items in cart (on top) and there won't be any checkouts based on phantom items.
From the moment that you save the cart items on the session and you have diferent sessions you have diferent carts.
To eliminate this, you need to have a common place for all session that you going to save your cart, and this is a table on a database. This common place are connected to the user ether with the user id, ether with the user cookie.
You'd have to AJAX-ify the shopping cart panel section of the page and use setTimeout() to refresh it at regular intervals.
Probably more effort than it's worth, however - if you're doing it right, clicking 'checkout' again on the 2nd page shouldn't cause duplicate transactions and the list will refresh on the next page load.
EDIT
And by "doing it right" I mean tracking cart items based on the user's Id, not just the Session object.
Your web app is based on HTTP protocol which open and close a connection just to satisfy an Http Web request. Now it is quite normal that if you open another two browser and check out on one of them you can still see the same page (infomration) on the other one. What I think you should do is just avoid that a user can check out twice or handle if someone is trying to check out an empty basket(which is your case if the user hit checkout on the second browser)
and it that case you can just show a message. If you go for the solution for which you should refresh at interval the page bear in mind what it could happen if the user open and log in into 250 different sessions: how many time your web app will be hit?
If you go for my suggestion all user session will get just an error page like: The bastek is empty.
I hope it makes sense

Categories

Resources