Where are the application events in the latest Asp.Net MVC 5 version?

Arshad Nazeer 21 Reputation points
2023-12-27T09:06:18.68+00:00

Hi.

I want to implement the requirement where the application counts the number of visitors to the site.

So, therefore I need to know the events like application start event which I believe is the way to count the number of user visits to my site.

I created a project .Net 8 version and asp.net mvc 5 version but could not find any application start event in the solution explorer. Earlier there was a configure method somewhere in the app to configure the app to the desired need. But couldn't find such configure method in the solution explorer now.

Can someone please help me regarding this to know how to configure the app to implement this task of counting number of visits to the site?

Thanks.

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 81,606 Reputation points Volunteer Moderator
    2023-12-27T18:06:24.9433333+00:00

    Application start fire once, when the application starts. These events are define the global.asax file.

    https://learn.microsoft.com/en-us/previous-versions/aspnet/ms178473(v=vs.100)

    to count visits you need to define what defines a visit. It’s common to use a browser session cookie. If it doesn’t exist create one and log the start of a browser session.

    if you are using asp.net session, then use session_start

    https://learn.microsoft.com/en-us/previous-versions/aspnet/ms178583(v=vs.100)

    0 comments No comments

  2. Anonymous
    2023-12-28T08:04:23.2866667+00:00

    Hi @Arshad Nazeer,

    You can count the number of user visits to your site in Program.cs like below:

    var app = builder.Build();
    int visitCount = 0;
    app.Use(async (context, next) =>
    {
        // Increment visit count for every request
        visitCount++;
        Console.WriteLine(visitCount);
        // Pass the request to the next middleware
        await next.Invoke();
    
        // You can perform additional actions after the response is sent
    });
    
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();
    
    app.UseAuthorization();
    
    app.MapControllers();
    
    app.Run();
    
    

    If you need to persist the visit count, you might consider using a database or another persistent storage solution.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Rena


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.