IHttpModule例子
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for RequestData
/// </summary>
public class RequestData : IHttpModule
{
public RequestData()
{
//
// TODO: Add constructor logic here
//
}
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
context.EndRequest += new EventHandler(context_EndRequest);
}
void context_EndRequest(object sender, EventArgs e)
{
HttpApplication httpApp = (HttpApplication)sender;
httpApp.Response.Output.WriteLine(
String.Format("<br>{0}", httpApp.Context.Items["requestStartTime"]));
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication httpApp = (HttpApplication)sender;
httpApp.Context.Items["requestStartTime"] = DateTime.Now;
System.Threading.Thread.Sleep(1000);
}
#endregion
}
web.config
<system.web>
<httpModules>
<add name="RequestData" type="RequestData"/>
</httpModules>
另一个例子:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Bikes_BikeInfo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable bikeList;
int bikeInfoId;
if (! Int32.TryParse(Request["ItemID"], out bikeInfoId))
{
bikeList = GetBikeList(0);
}
else
{
bikeList = GetBikeList(bikeInfoId);
}
Repeater1.DataSource = bikeList;
Page.DataBind();
}
private DataTable GetBikeList(int id)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Link", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("Description", Type.GetType("System.String")));
DataRow dr;
switch (id)
{
case 1: // Mountain Bikes
dr = dt.NewRow();
dr["Link"] = "PageDoesntExist.aspx";
dr["Description"] = "This bike is great for beginners...";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Link"] = "PageDoesntExist.aspx";
dr["Description"] = "Here is the bike for serious mountain bikers...";
dt.Rows.Add(dr);
break;
case 2: // road bikes
dr = dt.NewRow();
dr["Link"] = "PageDoesntExist.aspx";
dr["Description"] = "Beginer road bikes....";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Link"] = "PageDoesntExist.aspx";
dr["Description"] = "When you want to fly like the wind...";
dt.Rows.Add(dr);
break;
default:
dr = dt.NewRow();
dr["Link"] = "MountainBike.aspx";
dr["Description"] = "See our selection of mountain bikes";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Link"] = "RoadBike.aspx";
dr["Description"] = "See our selection of road bikes";
dt.Rows.Add(dr);
break;
}
return dt;
}
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
#endregion
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
if (app.Request.RawUrl.ToLower().Contains("/bikes/default.aspx"))
{
app.Context.RewritePath("BikeInfo.aspx", "", "");
}
else if (app.Request.RawUrl.ToLower().Contains("/bikes/mountainbike.aspx"))
{
app.Context.RewritePath("BikeInfo.aspx", "", "ItemID=1");
}
if (app.Request.RawUrl.ToLower().Contains("/bikes/roadbike.aspx"))
{
app.Context.RewritePath("BikeInfo.aspx", "", "ItemID=2");
}
}
<div>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<a href='<%# Eval("Link") %>'><%# Eval("Description") %></a><br />
</ItemTemplate>
</asp:Repeater>
</div>
目前维护的开源产品:https://gitee.com/475660