3

サーバーにインストールした HTTP モジュールがあります。奇妙なのは、それが機能することですが、たまに実行されていないことです。ログがあり、機能しない間はログに記録するコードに到達しません。IIS ログにもイベント ビューアにも何も表示されません。

    namespace RedirectModule
    {
        public class RedirectModule : System.Web.IHttpModule
        {
            private const string MobileUserAgent = "MobileUserAgentCacheKey";

            private const string 

STRING_TO_FIND = "info/lps";
        private const string STRING_TO_ADD = "/mobile";


        public void Dispose()
        {
            //clean-up code here.
        }

        public void Init(HttpApplication context)
        {          
            context.BeginRequest += context_BeginRequest;
        }
        private static object sync = new object();
        private void context_BeginRequest(object sender, EventArgs e)
        {

            try
            {


                HttpContext context = HttpContext.Current;


                string url = context.Request.Url.ToString().ToLower();

                if (!url.Contains(STRING_TO_FIND) || url.Contains(STRING_TO_FIND + STRING_TO_ADD))
                    return;
                Logger.Current.Log("Starting Redirect Phase");

                if (XmlToValues.IsMobile(context.Request.ServerVariables["HTTP_USER_AGENT"],
                                       GetCachedFile(context, "Settings.xml")))
                {
                    var mobileRedirect = GetRedirectUrl(url, STRING_TO_FIND, STRING_TO_ADD);
                    if (mobileRedirect != null)
                    {
                        Logger.Current.Log("Redirect to Mobile page");
                        context.Response.Redirect(mobileRedirect);

                    }
                }
                Logger.Current.Log("Web Page");
                Logger.Current.Log("End Begin Request");
            }
            catch (Exception ex)
            {
                if (ex is ThreadAbortException)
                    return;

                Logger.Current.LogError(ex);

            }
        }


        public static string GetRedirectUrl(string url, string strToFind, string strToAdd)
        {
            try
            {
                Logger.Current.Log("Get Redirect Url ");
                int idx = url.IndexOf(strToFind) + strToFind.Length;
                return url.Substring(0, idx) + strToAdd + url.Substring(idx);
            }
            catch (Exception ex)
            {

                Logger.Current.LogError(ex);


                return null;
            }
        }



        private XmlNodeList GetCachedFile(HttpContext context, string filePath)
        {
            try
            {
                Logger.Current.Log("GetCachedFile START");
                if (context.Cache[MobileUserAgent] == null)
                {
                    context.Cache[MobileUserAgent] = XmlToValues.GetMobileUserAgents(filePath);
                    Logger.Current.Log("Add Mobile File to Cache");
                }
                return (XmlNodeList)context.Cache[MobileUserAgent];
            }
            catch (Exception ex)
            {
                Logger.Current.LogError(ex);

                return null;
            }
        }

    }
}

と私の Web.Config:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>


  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="RedirectModule" />
      <add name="RedirectModule" type="RedirectModule.RedirectModule, RedirectModule" />


    </modules>
    <handlers>
      <remove name="Redirect" />
    </handlers>
    <validation validateIntegratedModeConfiguration="false"/>
  </system.webServer>
  <system.web>
    <httpModules>
      <add name="RedirectModule" type="RedirectModule.RedirectModule, RedirectModule" />
    </httpModules>
    <compilation debug="true">    
    </compilation>
  </system.web>
</configuration>

ps 面倒なので、web.config で log4net を取り出しました。

プロジェクトへのリンクは次のとおりです: http://www.sendspace.com/file/w42me5

これはリクエストされたページのマークアップで、index.htmnl というファイルにあります。

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<!-- no cache headers -->
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="no-cache">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="Cache-Control" content="no-cache">
<!-- end no cache headers -->
</head>
<body>
MOBILE
</body>
</html>
4

2 に答える 2

0

私は同様の問題を抱えていました... Webブラウザでキャッシュをオフにして、もう一度やり直してください。このリクエストのキャッシュをオフにするには、レスポンス ヘッダーを変更する必要があります。キャッシュ オプションの変更例

于 2012-08-01T12:45:17.530 に答える
0

おそらく、ブラウザとサーバーの間のどこかでキャッシュにアクセスしているようです。キャッシュが存在する可能性のある場所はたくさんあります。これを試して見つけるための一般的な手順を次に示します。

  • ブラウザー- ブラウザーは、キャッシュしたものを要求する必要はありません。 この質問には、送信されているリクエストを確認するためのさまざまなブラウザー用のツールがリストされています。
  • クライアント コンピューター- 要求がブラウザーから送信されている場合、プロキシ ( Squidなど) がキャッシュされたデータを返す場合があります。Fiddlerを使用して、HTTP 要求がクライアント コンピューターから送信されたかどうかを確認できます。
  • ネットワーク プロキシ- クライアント コンピューターと同じ考え方ですが、ネットワーク上のどこかのサーバーにプロキシが配置されているだけです。プロキシ サーバーで発信 HTTP トラフィックを分析して、要求が送信されているかどうかを判断する必要があります。
  • Web サーバー- リダイレクト モジュールは、新しいコンテンツをレンダリングする代わりに、キャッシュされたコンテンツを提供できます。この場合、ログに表示されるはずです。BeginRequest ハンドラーの最初にログ呼び出しを追加して、リクエストが実際にサーバーに届くかどうかを確認します。

キャッシュを防ぐために、キャッシュなしヘッダーをリクエストに追加できます (記事はこちら)。

        private void Application_EndRequest(Object source, EventArgs e) 
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            context.Response.ExpiresAbsolute = DateTime.Now.AddDays( -100 );
            context.Response.AddHeader( “pragma”, “no-cache” );
            context.Response.AddHeader( “cache-control”, “private” );
            context.Response.CacheControl = “no-cache”;
        }

編集

サーバーに到達するリクエストのデバッグ

応答の HTTP 200 は、キャッシュの問題がない可能性が非常に高いことを示しています。すべての要求が実際にサーバーに到達していることを確認するには、Application_BeginRequest ハンドラー (Global.asax 内) にログを追加して、すべての要求をログに記録し、HttpModule の context_BeginRequest によって生成されたログと比較してみてください。

//In Global.asax
private void Application_BeginRequest(Object source, EventArgs e) {
    Logger.Current.Log("Request made to Application_BeginRequest")
}

//In your HttpModule
    private void context_BeginRequest(object sender, EventArgs e)
    {
        //Log before any of your other code
        Logger.Current.Log("Request made to context_BeginRequest")

        try
        {
              // your code
        }
        catch (Exception ex)
        {
            // log the exception first in case this is the problem
            Logger.Current.LogError(ex);

            if (ex is ThreadAbortException)
                return;

        }
    }

結果の解釈

リクエストがモジュールに到達していない場合は、ログを確認してください。

  • 何も表示されない場合、リクエストはサーバーに到達していません。キャッシュについては上記を参照してください
  • Request made to Application_BeginRequestでログが終了する場合、モジュールは呼び出されていません。
  • Request made to context_BeginRequestで終了する場合、ロガーの前のモジュール コードにエラーがあり、一部のリクエストでクラッシュが発生しています。これは、null 参照 HttpContext.Current、または Request、オーバーフローの問題、またはその他のさまざまなものである可能性があります。これは、finally ステートメントでログに記録する必要があります。
于 2012-08-05T07:22:40.453 に答える