1

次のことを行う単純なリダイレクト モジュールを作成しています。

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

1.- URL の値を取得し、データベース内の URL テーブルにクエリを実行して、リダイレクトする正しい URL を取得します。

2.- Javascriptを使用してGoogle APIを使用してユーザーの緯度と経度を取得します

3.- 分離コードで緯度と経度を C# に戻すために、.ashx ハンドラーへの AJAX 呼び出しを行います。ハンドラーでは、ユーザーの緯度と経度を記録するためにデータベースに挿入します。

4.- データベースから以前に取得した正しい URL にユーザーをリダイレクトします。

次の問題が発生しており、回避策を見つけようとしています。

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
    }

コード ビハインドに追加する Javascript コードは、handler.ashxファイルへの AJAX 呼び出しを行います。このハンドラーは、ユーザーの緯度と経度を格納するために DB に挿入します。

例:

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
})

リダイレクトを行わなければ、すべて正常に動作します。リダイレクトを行うと、ハンドラーは挿入を行いません。リダイレクトは非常に高速に行われるため、ハンドラーには挿入を行う時間がないようです。私はそれを回避する他の方法を見つけようとしていましたが、見つかりませんでした:(。助けてくれてありがとう。

4

0 に答える 0