Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global asax redirect and log

Status
Not open for further replies.

adamroof

Programmer
Nov 5, 2003
1,107
0
0
US
Hi tektippers,

I have a check and redirect for the global.asax so that all pages start with not It also ensures that many of our domains we have registered get redirected to our main domain name.


My question is how can i log the original source url that they used so i can keep track of the usage of that non-primary domain name?
I have tried logging to the database but the db isnt initialized yet at this point in the begin request it seems.

Code:
protected void Application_BeginRequest(object sender, EventArgs e)
{
    string authority = HttpContext.Current.Request.Url.Authority;
    if (authority.IndexOf("localhost") != 0 && authority.IndexOf("[URL unfurl="true"]www.mydomain.com")[/URL] != 0)
    {
        Response.StatusCode = 301;
        Response.Redirect("[URL unfurl="true"]http://www.mydomain.com"[/URL] +
            HttpContext.Current.Request.Url.PathAndQuery, true);
    }
}

Any ideas would be greatly appreciated.

Adam
 
I have tried logging to the database but the db isnt initialized yet at this point in the begin request it seems.
a database is the way to go here. you can track all sorts of information about the request before redirecting them to the main domain.

the database has nothing to do with the website. ado.net is not related to asp.net in any form.

if you are using integrated security then you might have a problem because the user is not associated with the request yet, so the connection would fail. if this is a public site you shouldn't be using integrated security though.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Am i missing something? The redirects still work, but the db proc doesnt seem to be executing. Which is why i am wondering if im not connected to the db at this point. The similar visit log call on the master page works with the same call to my db helper

Code:
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            string authority = HttpContext.Current.Request.Url.Authority;
            if (authority.IndexOf("localhost") != 0 && authority.IndexOf("[URL unfurl="true"]www.mydomain.com")[/URL] != 0)
            {
                DB.ExecuteProc("sp_VisitorLog", new SqlParameter[] {
                    DB.Parameter("@page", SqlDbType.VarChar, 100, authority),
                    DB.Parameter("@ip", SqlDbType.VarChar, 50, HttpContext.Current.Request.UserHostAddress),
                    DB.Parameter("@refer", SqlDbType.VarChar, 150, (HttpContext.Current.Request.UrlReferrer == null) ? "" : HttpContext.Current.Request.UrlReferrer.ToString()),
                    DB.Parameter("@SiteID", SqlDbType.Int, 1),
                    DB.Parameter("@Locale", SqlDbType.VarChar, 50, "en-US")
                });

                Response.StatusCode = 301;
                Response.Redirect("[URL unfurl="true"]http://www.mydomain.com"[/URL] +
                    HttpContext.Current.Request.Url.PathAndQuery, true);
            }
        }
 
if the db call fails an exception should be thrown. if not it's 1 of three things
1. you don't execute the command (only define it)
2. you are swallowing an exception somewhere.
3. the code is never reached. it's either bypassed, or redirected before executing.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
or...

4. Your primary if statement actually isnt resulting true as you thought it should. (actually would fall under your #3)

The above code works as designed.

We have about 20 domain names. The original 5 we had our hosting provider create redirects for us on the IIS server itself many moons ago, have to go back and see what they did to ask them to undo it. They did that for us before we had control over the code and implemented our own "improvements".

1. > no redirect, No Log, as intended
2. domain.com > > Log Success!
3. old_domain.com > > No Log!
4. new_domain.com > > Log Success!

So they have a rewrite already going for the 5 "old_domains". and the few new ones work as intended, but i was trying 2 out of the 5 names that dont log and considered it a failure. The difference is that it lands on index.aspx in the URL, now just need to find out why.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top