0

.net HttpModule に関する問題が発生しています。Web サイトのリクエストをキャプチャする HttpModule を作成します。ajax リクエストを除くすべての Web サイト ページをキャプチャできます。

これが私のweb.configです。Moduler.FilterModuler は私が作成したものです。

   `<httpModules>
       <add name="handleTimeOut" type="Moduler.FilterModuler"/>
   </httpModules>

    <system.webServer>
       <validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="ScriptModule"/>
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
 </modules>
  <handlers>
    <remove name="WebServiceHandlerFactory-Integrated"/>
    <remove name="ScriptHandlerFactory"/>
    <remove name="ScriptHandlerFactoryAppServices"/>
        <remove name="ScriptResource"/>
        <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptResource" verb="GET,HEAD" path="ScriptResource.axd" preCondition="integratedMode" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</handlers></system.webServer>`

これが私のモジュールです。

    namespace Moduler
    {
        public class FilterModuler : IHttpModule, IReadOnlySessionState
        {
    private class ResponseCaptureStream : Stream
    {
        private readonly Stream _streamToCapture;
        private readonly Encoding _responseEncoding;

        private string _streamContent;
        public string StreamContent
        {
            get { return _streamContent; }
            private set
            {
                _streamContent = value;
            }
        }

        public ResponseCaptureStream(Stream streamToCapture, Encoding responseEncoding)
        {
            _responseEncoding = responseEncoding;
            _streamToCapture = streamToCapture;

        }

        public override bool CanRead
        {
            get { return _streamToCapture.CanRead; }
        }

        public override bool CanSeek
        {
            get { return _streamToCapture.CanSeek; }
        }

        public override bool CanWrite
        {
            get { return _streamToCapture.CanWrite; }
        }

        public override void Flush()
        {
            _streamToCapture.Flush();
        }

        public override long Length
        {
            get { return _streamToCapture.Length; }
        }

        public override long Position
        {
            get
            {
                return _streamToCapture.Position;
            }
            set
            {
                _streamToCapture.Position = value;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return _streamToCapture.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return _streamToCapture.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            _streamToCapture.SetLength(value);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            string str = _responseEncoding.GetString(buffer, offset, count);
            _streamContent += str;
        }

        public override void Close()
        {
            _streamContent = _streamContent.Replace("$Social Work$", "hello world");
            byte[] data = _responseEncoding.GetBytes(_streamContent);
            _streamToCapture.Write(data, 0, data.Length);
            _streamToCapture.Close();
            base.Close();
        }
    }
    public void Dispose()
    {

    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest +=new EventHandler(context_BeginRequest);
        context.ReleaseRequestState += new EventHandler(context_ReleaseRequestState);
    }

    protected void context_ReleaseRequestState(object sender, EventArgs e)
    {
        HttpApplication application = (HttpApplication)sender;
        string[] temp = application.Request.CurrentExecutionFilePath.Split('.');
        if (temp.Length > 0 && temp[temp.Length - 1].ToLower() == "aspx")
        {
            application.Response.Filter = new ResponseCaptureStream(application.Response.Filter, application.Response.ContentEncoding);
        }

    }

    protected void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication application = (HttpApplication)sender;
        using (StreamWriter sw = new StreamWriter("C:\\work\\request.txt"))
        {
            sw.Write(application.Request.CurrentExecutionFilePath);
        }
    }

    public FilterModuler()
    {
        //
        // TODO: Add constructor logic here
        //
    }
}

}`

4

1 に答える 1

0

この例を使用して、HttpModule イベント solicudes AJAX をキャプチャできなかった同様の問題を解決します。


Ajax 呼び出しを処理するための HttpModule

于 2012-07-16T23:02:00.977 に答える