62,074
社区成员
发帖
与我相关
我的任务
分享
<!--配置文件-->
<add name="UrlReWrite" type="UrlProcess.UrlReWrite,UrlProcess"/>
public class UrlReWrite : IHttpModule
{
HttpApplication application = null;
#region IHttpModule 成员
public void Dispose()
{
//throw new NotImplementedException();
}
public void Init(HttpApplication context)
{
context.BeginRequest += new System.EventHandler(context_BeginRequest);
application = context;
}
private void context_BeginRequest(object sender, System.EventArgs e)
{
try
{
string requestUrl = application.Context.Request.Path;
string[] tmpUrl = requestUrl.Split('/');
string shopname = tmpUrl[tmpUrl.Length-1];
if (shopname.Split('.').Length == 1)// && (tmpUrl[tmpUrl.Length - 2] == "bbgang.com" || tmpUrl[tmpUrl.Length - 2]=="www.bbgang.com")
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["SQLConn"].ToString());
conn.Open();
SqlCommand cmd = new SqlCommand("select Id,Account from shop where [Account]='" + shopname + "'", conn);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
string shopid = sdr[0].ToString();
application.Context.RewritePath("~/shoppages/shop.aspx?sid=" + shopid);
}
conn.Close();
}
}
catch (Exception ex) {
//
}
}
#endregion
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
/// <summary>
///HttpModule 的摘要说明
/// </summary>
public class UrlReWriter: IHttpModule
{
public UrlReWriter()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public void Dispose()
{
//这里写Dispose代码
}
public void Init(HttpApplication context)
{
//context.BeginRequest是开始处理HTTP管线请求时发生的事件
context.BeginRequest += new EventHandler(context_BeginRequest);
//context.Error是当处理过程中发生异常时产生的事件
context.Error += new EventHandler(context_Error);
}
void context_Error(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
context.Response.Write("<html>");
context.Response.Write("<head><title>出错了~~</title>");
context.Response.Write("</head>");
context.Response.Write("<body style=\"font-size:14px;align:center;BORDER:#bfceff 1px solid; height:18;FILTER:progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColorStr='#ddecfb',endColorStr='#ffffff'); width:180;\">");
context.Response.Write("<div name=\"errormessage\" ><p>");
context.Response.Write("对不起出错了:<br /><br />");
context.Response.Write(HttpUtility.HtmlEncode(context.Server.GetLastError().ToString()));
context.Response.Write("</p></div>");
context.Response.Write("</body></html>");
context.Response.End();
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpResponse response = context.Response;
string path = context.Request.Path;
string file = System.IO.Path.GetFileName(path);
//重写后的URL地址
Regex regex = new Regex("Down-(\\d+).aspx", RegexOptions.Compiled);
Match match = regex.Match(file);
//如果满足URL地址重写的条件
if (match.Success)
{
string userId = match.Groups[1].Value;
string rewritePath = "Down.aspx?aid=" + userId;
//将其按照UserInfo.aspx?UserId=123这样的形式重写,确保能正常执行
context.RewritePath(rewritePath);
}
}
}
<httpModules>
<add name="UrlReWriter" type="UrlReWriter"/><!--接管请求,地址重写-->
</httpModules>