ASP.NET:HttpModuleをカスタマイズする際の注意点

4238 ワード

      HttpModule   。            :  Module   Request     ,   Module    Response     ,                 。
        ,   BeginRequest          。     ,  Request     ,                。
 
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



using System.IO;

using System.Xml.Linq;



namespace WebApp

{

    /// <summary>

    ///        ,                  

    ///   :   

    /// </summary>



    public class MyModule:IHttpModule

    {

        #region IHttpModule   



        public void Dispose()

        {

            

        }



        public void Init(HttpApplication context)

        {





            context.BeginRequest += (o1, e1) =>

            {



                //  Module    Request      ,           (    )   ,      



                HttpApplication app = (HttpApplication)o1;

 if (app.Request.Url.AbsolutePath.EndsWith("aspx"))                     app.Response.Write("     <hr />");

                

            };







            context.EndRequest += (o2, e2) =>

            {

                HttpApplication app = (HttpApplication)o2;

                if (app.Request.Url.AbsolutePath.EndsWith("aspx"))

                    app.Context.Response.Write("<hr />     ");



                //          ,          

                string logFile = app.Server.MapPath("Log.xml");

                XDocument doc = null;

                if (!File.Exists(logFile))

                {

                    doc = new XDocument(

                        new XElement("Logs"));



                }

                else

                    doc = XDocument.Load(logFile);



                XElement item =

                    new XElement("Item",

                        new XAttribute("User", app.User.Identity.Name),

                        new XAttribute("Time", DateTime.Now),

                        new XAttribute("Path", app.Request.Path));

                doc.Root.Add(item);

                doc.Save(logFile);







            };

        }



        #endregion

    }

}


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }