日期:2014-05-18 浏览次数:20698 次
/// <summary>
/// Begin Request
/// </summary>
/// <param name="sender">HttpApplication</param>
/// <param name="e">Event Args.</param>
public void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
String Prefix = "/";
if (System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath != "/")
{
Prefix = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath + "/";
}
Regex r1 = new Regex(Prefix + @"([0-9a-zA-Z\-]*)", RegexOptions.IgnoreCase);
String Query = app.Request.Url.Query;
if (Query.StartsWith("?"))
{
Query = "&" + Query.Substring(1, Query.Length - 1);
}
if (r1.Match(app.Request.Path).Success)
{
var agentno = DataAccess.Provider.SzAgentHelper.GetAgentNoByName(r1.Match(app.Request.Path).Groups[1].Value);
if (agentno != null)
app.Context.RewritePath("~/blog/agent.aspx?id=E-" + agentno, false);
}
}
void Application_BeginRequest(Object sender, EventArgs e)
{
String oldUrl = System.Web.HttpContext.Current.Request.RawUrl;
//其中()最为一个整体,.+任意多个除了()以外的字符,^与字符串开始的地方匹配
//d+任意多个数字,w+任意多个字母或数字或下划线
String pattern = @"^(.+)/test4/(d+).aspx/(w+)";
String replace = @"~/test4.aspx?NID=$1&id=$2&uid=$3";
if(Regex.IsMatch(oldUrl,pattern,RegexOptions.IgnoreCase | RegexOptions.Compiled))
{
String newUrl=Regex.Replace(oldUrl,pattern,replace,RegexOptions.Compiled| RegexOptions.IgnoreCase);
Context.RewritePath(newUrl);
}
}
------解决方案--------------------
伪静态