views:

18

answers:

2

I have a multiple web sits asp.net application. In this application different domains using the same pages. All pages inherit from base class named: PageBase wich inherit from System.Web.UI.Page. By using: HttpContext.Current.Request.ServerVariables["HTTP_HOST"] i cen determine what is the domain and then get all the info i need for this domain and everything is working good.

My problem begin when i want to use different visitor counter for each site based on session. Because Global.asax have Global events: Session_Start Session_End simple counter will count all visitors on all sites together. I try to make code behind for the Global.asax in different class but i cold not get in that class the PageBase(System.Web.UI.Page) web site info.

I will be very thankful for any ideas to solve this problem

cheinan

A: 

I am assuming that you are able to browse from one "site" to the other within the same session and that there is only one session created.

In this case, you need to add the following to your Session:

    Session["CountedHosts"] = new List<string>();

Then, on your base page, add the following:

var host = Request.ServerVariables["HTTP_HOST"];
var countedHosts = Session["CountedHosts"] as List<string>;
if (countedHosts != null && !countedHosts.Contains(host))
{
    countedHosts.Add(host);
}

Then on session end, record each host that was visited.

var countedHosts = Session["CountedHosts"] as List<string>;
if (countedHosts != null)
{
     foreach (var host in countedHosts)
     {
          //Log it

      }
}
Daniel Dyson
A: 

I am not able to browse from one "site" to the other within the same session each site have is on

different session created.

but i am very thankful to you because you gave me en idea how to solv this problem

here is what i did: i created counter class with Dictionary "onlineList" were i automatic creat for each site a key:

public abstract class counter{
public static Dictionary<string, int> onlineList = new Dictionary<string, int>();

 //do add one count
public static void doSiteCountOn(string siteID)
{
    if (onlineList.ContainsKey(siteID))
    {
        onlineList[siteID] += 1;
    }
    else
    {
        onlineList.Add(siteID, 1);
    }
}

//do less one count
public static void doSiteCountOff(string siteID)
{
    if (onlineList.ContainsKey(siteID))
    {
        onlineList[siteID] -= 1;
    }
    else
    {
        onlineList.Add(siteID, 0);
    }
}

//get the count
public static string onlineCount(string siteID)
{
    if (onlineList.ContainsKey(siteID))
    {
        return onlineList[siteID].ToString();
    }
    else
    {
        return "0";
    }
}

//reset the count if needed
public static void resetCount(string siteID)
{
    if (onlineList.ContainsKey(siteID))
    {
        onlineList[siteID] = 0;
    }
}}

on my base page i check if there is Session["siteID"] and if not i start one and make my counter class to add 1 to the site counter:

if (Session["siteID"] == null){
Session["siteID"] = _siteID;
counter.doSiteCountOn(_siteID);}

and finaly on my Session_End i do one count less:

void Session_End(object sender, EventArgs e){
if (Session["siteID"] != null)
    {
        counter.doSiteCountOff(Session["siteID"].ToString());
    }}

thank for your halp and sorry for my late respons

cheinan