1

I am writing a simple redirection module that does the following:

##sample URL: http://localhost/redir.aspx?r=1234##

1.- I get the value of the URL and I query my URL table in my database to retrieve the correct URL to be redirected.

2.- I get the user latitude and longitude using Google API using Javascript

3.- I make an AJAX call to an .ashx handler to pass the latitude and longitude back to C# in the codebehind. In the handler, I do an insert to the database to log the users latitude and longitude.

4.- I redirect the user to the correct URL that I retrieved earlier from my database.

I am having the following problem and I am trying to find a workaround:

redir.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
          string r = Request.QueryString["r"];
          int result = CallDatabase(r);  //This method makes a call to my database and gets some value from my table that I need to include on my javascript. After that, I use RegisterClientScriptBlock to insert Google API Javascript to get the Geolocation from the user. In the javascript I include an extra parameter (val) that I get from CallDatabase.
          if (result > 0) 
              GetUrl(result); //Now I query the DB and get the URL to do the redirection. Once I have the URL from the DB, I do a response.redirect
    }

The Javascript code that I am adding in the code behind makes an AJAX call to my handler.ashx file. This handler does an insert into the DB to store the user latitude and longitude.

example:

function success(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    var pullurl = '/handler.ashx?val=TN100&latlng=' + lat + ',' + lng;
    get(pullurl);
}

function get(url) {
    $.ajax({
        type: "GET",
    url: url,
    success: handleData
})

Everything works fine if I do not do the redirection. If I do the redirection, the handler does not do the insert. As the redirection is done extremely fast, it seems the handler does not have time to do the insert. I have been trying to find other ways around it but I haven't found any :(. Much appreciated any help thanks.

4

0 回答 0