Pergunta

Clickhere Global.asax.cs

      namespace WebApplication7
     {
    public class Global : System.Web.HttpApplication
      {
    private static int countdays=0;

    protected void Application_Start(object sender, EventArgs e)
    {
        countdays = 0;
    }

    protected void Session_Start(object sender, EventArgs e)
    {
        countdays += 1;
    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {

    }

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {

    }

    protected void Application_Error(object sender, EventArgs e)
    {

    }

    protected void Session_End(object sender, EventArgs e)
    {
        countdays -= 1;
    }

    protected void Application_End(object sender, EventArgs e)
    {

    }
    public static int CountNo { get { return countdays; } }
  }
}

Global.apsx

  <body>
  <form id="fromHitCounter" method="post" runat="server">
  Total number of days since the Web server started:
 <asp:label id="lblCount" runat="server"></asp:label><br />
 </form>
 </body>

Global.aspx.cs

      private void Page_Load(object sender, System.EventArgs e)

        {

      int Countdays = HitCounters.Global.Countdays;//Hit counter does not exist  


      lblCount.Text = Countdays.ToString();

        }

How to calculate days counter in asp.net using global.asax file,In Global.aspx.cs iam getting the error hit counter does not exist in the current context

Foi útil?

Solução

I'm not going to ask why, I probably won't like the reason you give me. However you're not counting days here. You are counting session starts.

What you really want to do is something like this:

public class Global : System.Web.HttpApplication
{

    private static DateTime started;
    private static int days;

    protected void Application_Start(object sender, EventArgs e)
    {
        started = DateTime.UtcNow;
        days = 0;
    }

    protected void Session_Start(object sender, EventArgs e)
    {
        TimeSpan ts = DateTime.UtcNow - started;
        days = (int)ts.TotalDays;
    }

    ...

  }
}

However, that's assuming the session events fire and you're also overlooking the fact the applications can and do get unloaded, your application may not stay loaded even a single day.

Your link points to counting the number of website visits which isn't the same as counting days or getting how long the web server has been running. It is also a very poor attempt because it does not take into account repeat visits from the same user etc and is not really persistent across app domain unloads.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top